From 0b6efc3fdb378c47c6f17501032e0523d2a40463 Mon Sep 17 00:00:00 2001 From: Egor Vasyakin Date: Wed, 18 Aug 2021 14:30:15 +0300 Subject: [PATCH] Update ActiveRecord.php --- src/ActiveRecord.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 8f45894..065c06c 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -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 ?? []) + ); } } @@ -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; } @@ -206,7 +209,7 @@ 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(); @@ -214,7 +217,7 @@ public function save() $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'); }