|
@@ -72,7 +72,7 @@
|
|
|
throw new \Exception('Instance already cached');
|
|
|
}
|
|
|
foreach($idOrData as $key => $val){
|
|
|
- $this->_data[$key] = $val;
|
|
|
+ $this->set($key, $val);
|
|
|
}
|
|
|
}else{
|
|
|
if(self::cached($idOrData)){
|
|
@@ -107,7 +107,8 @@
|
|
|
case 'has_one':case 'has_many':case 'belongs_to':
|
|
|
return self::$aliases[$this->name][$name];
|
|
|
case 'id':
|
|
|
- return $this->get(static::primary_key());
|
|
|
+ $pk = static::primary_key();
|
|
|
+ return $this->has($pk) ? $this->get($pk) : null;
|
|
|
default:
|
|
|
throw new \Exception('Unknown property '.$name);
|
|
|
}
|
|
@@ -311,13 +312,15 @@
|
|
|
$data[$key] = null;
|
|
|
}
|
|
|
}
|
|
|
- if(!is_null($this->id) && !in_array(static::primary_key(), $this->_changed)){
|
|
|
- static::table()->update($data, [
|
|
|
- static::primary_key() => $this->id
|
|
|
+ $pk = static::primary_key();
|
|
|
+ $table = static::table();
|
|
|
+ if($this->has($pk) && !is_null($this->id) && !in_array($pk, $this->_changed)){
|
|
|
+ $table->update($data, [
|
|
|
+ $pk => $this->id
|
|
|
]);
|
|
|
}else{
|
|
|
- static::table()->insert($data);
|
|
|
- $this->_data[static::primary_key()] = self::$pdo->lastInsertId();
|
|
|
+ $table->insert($data);
|
|
|
+ $this->_data[$pk] = self::$pdo->lastInsertId();
|
|
|
}
|
|
|
foreach($this->_related as $related){
|
|
|
$related->save();
|