diff --git a/extensions/elasticsearch/ActiveRecord.php b/extensions/elasticsearch/ActiveRecord.php index 097cbc46ab8..4db97ef7f6f 100644 --- a/extensions/elasticsearch/ActiveRecord.php +++ b/extensions/elasticsearch/ActiveRecord.php @@ -395,8 +395,9 @@ public function insert($runValidation = true, $attributes = null, $options = ['o } $this->_version = $response['_version']; $this->_score = null; - $this->setOldAttributes($values); + $this->afterSave(true); + $this->setOldAttributes($values); return true; } diff --git a/extensions/elasticsearch/CHANGELOG.md b/extensions/elasticsearch/CHANGELOG.md index 0ee84eb1648..c74820e2902 100644 --- a/extensions/elasticsearch/CHANGELOG.md +++ b/extensions/elasticsearch/CHANGELOG.md @@ -9,6 +9,7 @@ Yii Framework 2 elasticsearch extension Change Log - Enh #1313: made index and type available in `ActiveRecord::instantiate()` to allow creating records based on elasticsearch type when doing cross index/type search (cebe) - Enh #1382: Added a debug toolbar panel for elasticsearch (cebe) - Enh #1765: Added support for primary key path mapping, pk can now be part of the attributes when mapping is defined (cebe) +- Enh #2892: ActiveRecord dirty attributes are now reset after call to `afterSave()` so information about changed attributes is available in `afterSave`-event (cebe) - Chg #1765: Changed handling of ActiveRecord primary keys, removed getId(), use getPrimaryKey() instead (cebe) - Chg #2281: Renamed `ActiveRecord::create()` to `populateRecord()` and changed signature. This method will not call instantiate() anymore (cebe) - Chg #2146: Removed `ActiveRelation` class and moved the functionality to `ActiveQuery`. diff --git a/extensions/mongodb/ActiveRecord.php b/extensions/mongodb/ActiveRecord.php index 66f9267bddb..f157fd96f23 100644 --- a/extensions/mongodb/ActiveRecord.php +++ b/extensions/mongodb/ActiveRecord.php @@ -242,10 +242,9 @@ protected function insertInternal($attributes = null) } $newId = static::getCollection()->insert($values); $this->setAttribute('_id', $newId); - foreach ($values as $name => $value) { - $this->setOldAttribute($name, $value); - } + $this->afterSave(true); + $this->setOldAttributes($values); return true; } @@ -262,7 +261,6 @@ protected function updateInternal($attributes = null) $values = $this->getDirtyAttributes($attributes); if (empty($values)) { $this->afterSave(false); - return 0; } $condition = $this->getOldPrimaryKey(true); @@ -281,10 +279,10 @@ protected function updateInternal($attributes = null) throw new StaleObjectException('The object being updated is outdated.'); } + $this->afterSave(false); foreach ($values as $name => $value) { $this->setOldAttribute($name, $this->getAttribute($name)); } - $this->afterSave(false); return $rows; } diff --git a/extensions/mongodb/file/ActiveRecord.php b/extensions/mongodb/file/ActiveRecord.php index eec4b7bef93..085db599f95 100644 --- a/extensions/mongodb/file/ActiveRecord.php +++ b/extensions/mongodb/file/ActiveRecord.php @@ -147,10 +147,9 @@ protected function insertInternal($attributes = null) $newId = $collection->insert($values); } $this->setAttribute('_id', $newId); - foreach ($values as $name => $value) { - $this->setOldAttribute($name, $value); - } + $this->afterSave(true); + $this->setOldAttributes($values); return true; } @@ -167,7 +166,6 @@ protected function updateInternal($attributes = null) $values = $this->getDirtyAttributes($attributes); if (empty($values)) { $this->afterSave(false); - return 0; } @@ -219,10 +217,10 @@ protected function updateInternal($attributes = null) } } + $this->afterSave(false); foreach ($values as $name => $value) { $this->setOldAttribute($name, $this->getAttribute($name)); } - $this->afterSave(false); return $rows; } diff --git a/extensions/redis/ActiveRecord.php b/extensions/redis/ActiveRecord.php index ae9a3d3ded3..77883ec7b31 100644 --- a/extensions/redis/ActiveRecord.php +++ b/extensions/redis/ActiveRecord.php @@ -146,8 +146,8 @@ public function insert($runValidation = true, $attributes = null) } $db->executeCommand('HMSET', $args); - $this->setOldAttributes($values); $this->afterSave(true); + $this->setOldAttributes($values); return true; } diff --git a/extensions/redis/CHANGELOG.md b/extensions/redis/CHANGELOG.md index 8c88fe574e3..ac85a2cf66a 100644 --- a/extensions/redis/CHANGELOG.md +++ b/extensions/redis/CHANGELOG.md @@ -6,6 +6,7 @@ Yii Framework 2 redis extension Change Log - Bug #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder) - Enh #1773: keyPrefix property of Session and Cache is not restricted to alnum characters anymore (cebe) +- Enh #2892: ActiveRecord dirty attributes are now reset after call to `afterSave()` so information about changed attributes is available in `afterSave`-event (cebe) - Chg #2281: Renamed `ActiveRecord::create()` to `populateRecord()` and changed signature. This method will not call instantiate() anymore (cebe) - Chg #2146: Removed `ActiveRelation` class and moved the functionality to `ActiveQuery`. All relational queries are now directly served by `ActiveQuery` allowing to use diff --git a/extensions/sphinx/ActiveRecord.php b/extensions/sphinx/ActiveRecord.php index bd9832a6f1c..87efa0e5ae2 100644 --- a/extensions/sphinx/ActiveRecord.php +++ b/extensions/sphinx/ActiveRecord.php @@ -416,10 +416,9 @@ private function insertInternal($attributes = null) if (!$command->execute()) { return false; } - foreach ($values as $name => $value) { - $this->setOldAttribute($name, $value); - } + $this->afterSave(true); + $this->setOldAttributes($values); return true; } @@ -512,7 +511,6 @@ protected function updateInternal($attributes = null) $values = $this->getDirtyAttributes($attributes); if (empty($values)) { $this->afterSave(false); - return 0; } @@ -554,10 +552,10 @@ protected function updateInternal($attributes = null) } } + $this->afterSave(false); foreach ($values as $name => $value) { $this->setOldAttribute($name, $this->getAttribute($name)); } - $this->afterSave(false); return $rows; } diff --git a/extensions/sphinx/CHANGELOG.md b/extensions/sphinx/CHANGELOG.md index cd2496789e8..2fed0efb65c 100644 --- a/extensions/sphinx/CHANGELOG.md +++ b/extensions/sphinx/CHANGELOG.md @@ -7,6 +7,7 @@ Yii Framework 2 sphinx extension Change Log - Bug #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder) - Bug #2160: SphinxQL does not support `OFFSET` (qiangxue, romeo7) - Enh #1398: Refactor ActiveRecord to use BaseActiveRecord class of the framework (klimov-paul) +- Enh #2892: ActiveRecord dirty attributes are now reset after call to `afterSave()` so information about changed attributes is available in `afterSave`-event (cebe) - Chg #2281: Renamed `ActiveRecord::create()` to `populateRecord()` and changed signature. This method will not call instantiate() anymore (cebe) - Chg #2146: Removed `ActiveRelation` class and moved the functionality to `ActiveQuery`. All relational queries are now directly served by `ActiveQuery` allowing to use diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index f8df6ae733e..2416f2dc25a 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -156,6 +156,7 @@ Yii Framework 2 Change Log - Enh #2735: Added support for `DateTimeInterface` in `Formatter` (ivokund) - Enh #2756: Added support for injecting custom `isEmpty` check for all validators (qiangxue) - Enh #2775: Added `yii\base\Application::bootstrap` and `yii\base\BootstrapInterface` to support running bootstrap classes when starting an application (qiangxue) +- Enh #2892: ActiveRecord dirty attributes are now reset after call to `afterSave()` so information about changed attributes is available in `afterSave`-event (cebe) - Enh: Added support for using arrays as option values for console commands (qiangxue) - Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark) - Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue) diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 6534dd6ac25..a9b563c1fc2 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -450,10 +450,9 @@ protected function insertInternal($attributes = null) } } } - foreach ($values as $name => $value) { - $this->setOldAttribute($name, $value); - } + $this->afterSave(true); + $this->setOldAttributes($values); return true; } diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index a62308cbd1c..0addba3cbbe 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -564,7 +564,6 @@ public function getDirtyAttributes($names = null) } } } - return $attributes; } @@ -654,7 +653,6 @@ public function update($runValidation = true, $attributes = null) if ($runValidation && !$this->validate($attributes)) { return false; } - return $this->updateInternal($attributes); } @@ -684,8 +682,7 @@ public function updateAttributes($attributes) $attrs[] = $name; } } - - return $this->update(false, $attrs); + return $this->updateInternal($attrs); } /** @@ -700,7 +697,6 @@ protected function updateInternal($attributes = null) $values = $this->getDirtyAttributes($attributes); if (empty($values)) { $this->afterSave(false); - return 0; } $condition = $this->getOldPrimaryKey(true); @@ -719,10 +715,10 @@ protected function updateInternal($attributes = null) throw new StaleObjectException('The object being updated is outdated.'); } + $this->afterSave(false); foreach ($values as $name => $value) { $this->_oldAttributes[$name] = $this->_attributes[$name]; } - $this->afterSave(false); return $rows; } @@ -751,7 +747,6 @@ public function updateCounters($counters) $this->_attributes[$name] += $value; $this->_oldAttributes[$name] = $this->_attributes[$name]; } - return true; } else { return false;