Skip to content

Commit

Permalink
Update ActiveRecord.php
Browse files Browse the repository at this point in the history
  • Loading branch information
evasyakin committed Aug 18, 2021
1 parent e6310db commit 0b6efc3
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ public function getUpdatedProperties(): array
return $props;
} else {
$state = static::getDb()->identityMapGetState($this, $pk);
return array_diff($props, $state ?? []);
// return array_diff($props, $state ?? []);
return array_merge(
array_fill_keys(array_keys(array_diff($state ?? [], $props)), null),
array_diff($props, $state ?? [])
);
}
}

Expand All @@ -196,8 +200,7 @@ public function getUpdatedProperties(): array
*/
public function save()
{
$props = $this->getUpdatedProperties();
if (empty($props)) {
if (empty($this->getUpdatedProperties())) {
$this->hook('nothingSave');
return $this;
}
Expand All @@ -206,15 +209,15 @@ public function save()
$pk = static::primaryKey();
if (empty($this->$pk)) {
$this->hook('beforeInsert');
static::getDb(true)->insert(static::tableName(), $props);
static::getDb(true)->insert(static::tableName(), $this->getUpdatedProperties());
$this->$pk = static::lastInsertId();
if (empty($this->$pk)) {
throw new LastInsertIdUndefinedException();
}
$this->hook('afterInsert');
} else {
$this->hook('beforeUpdate');
static::getDb(true)->update(static::tableName(), $props)
static::getDb(true)->update(static::tableName(), $this->getUpdatedProperties())
->where("$pk = ?", [$this->$pk])->one();
$this->hook('afterUpdate');
}
Expand Down

0 comments on commit 0b6efc3

Please sign in to comment.