From f9febd00870e5a2aa8d45eb6e12f66bb636c5140 Mon Sep 17 00:00:00 2001 From: Vladimir Zbrailov Date: Fri, 27 Jun 2014 20:10:43 +0400 Subject: [PATCH] unify changedAttributes of afterSave in extensions --- extensions/elasticsearch/ActiveRecord.php | 3 ++- extensions/mongodb/ActiveRecord.php | 7 +++++-- extensions/mongodb/file/ActiveRecord.php | 7 +++++-- extensions/redis/ActiveRecord.php | 3 ++- extensions/sphinx/ActiveRecord.php | 9 ++++++--- framework/CHANGELOG.md | 2 +- framework/UPGRADE.md | 1 + 7 files changed, 22 insertions(+), 10 deletions(-) diff --git a/extensions/elasticsearch/ActiveRecord.php b/extensions/elasticsearch/ActiveRecord.php index c4dc26c948..4c0ed983c1 100644 --- a/extensions/elasticsearch/ActiveRecord.php +++ b/extensions/elasticsearch/ActiveRecord.php @@ -416,8 +416,9 @@ class ActiveRecord extends BaseActiveRecord $this->_version = $response['_version']; $this->_score = null; + $changedAttributes = array_fill_keys(array_keys($values), null); $this->setOldAttributes($values); - $this->afterSave(true, $values); + $this->afterSave(true, $changedAttributes); return true; } diff --git a/extensions/mongodb/ActiveRecord.php b/extensions/mongodb/ActiveRecord.php index 99a4ad0300..fe954effdd 100644 --- a/extensions/mongodb/ActiveRecord.php +++ b/extensions/mongodb/ActiveRecord.php @@ -224,8 +224,9 @@ abstract class ActiveRecord extends BaseActiveRecord $this->setAttribute('_id', $newId); $values['_id'] = $newId; + $changedAttributes = array_fill_keys(array_keys($values), null); $this->setOldAttributes($values); - $this->afterSave(true, $values); + $this->afterSave(true, $changedAttributes); return true; } @@ -260,10 +261,12 @@ abstract class ActiveRecord extends BaseActiveRecord throw new StaleObjectException('The object being updated is outdated.'); } + $changedAttributes = []; foreach ($values as $name => $value) { + $changedAttributes[$name] = $this->getOldAttribute($name); $this->setOldAttribute($name, $this->getAttribute($name)); } - $this->afterSave(false, $values); + $this->afterSave(false, $changedAttributes); return $rows; } diff --git a/extensions/mongodb/file/ActiveRecord.php b/extensions/mongodb/file/ActiveRecord.php index fe8575d016..5368e30a65 100644 --- a/extensions/mongodb/file/ActiveRecord.php +++ b/extensions/mongodb/file/ActiveRecord.php @@ -127,8 +127,9 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord $this->setAttribute('_id', $newId); $values['_id'] = $newId; + $changedAttributes = array_fill_keys(array_keys($values), null); $this->setOldAttributes($values); - $this->afterSave(true, $values); + $this->afterSave(true, $changedAttributes); return true; } @@ -196,10 +197,12 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord } } + $changedAttributes = []; foreach ($values as $name => $value) { + $changedAttributes[$name] = $this->getOldAttribute($name); $this->setOldAttribute($name, $this->getAttribute($name)); } - $this->afterSave(false, $values); + $this->afterSave(false, $changedAttributes); return $rows; } diff --git a/extensions/redis/ActiveRecord.php b/extensions/redis/ActiveRecord.php index 8c1e79a510..aa7691fc48 100644 --- a/extensions/redis/ActiveRecord.php +++ b/extensions/redis/ActiveRecord.php @@ -124,8 +124,9 @@ class ActiveRecord extends BaseActiveRecord } $db->executeCommand('HMSET', $args); + $changedAttributes = array_fill_keys(array_keys($values), null); $this->setOldAttributes($values); - $this->afterSave(true, $values); + $this->afterSave(true, $changedAttributes); return true; } diff --git a/extensions/sphinx/ActiveRecord.php b/extensions/sphinx/ActiveRecord.php index 4c743d0f91..ef4909dc20 100644 --- a/extensions/sphinx/ActiveRecord.php +++ b/extensions/sphinx/ActiveRecord.php @@ -395,8 +395,9 @@ abstract class ActiveRecord extends BaseActiveRecord return false; } - $this->setOldAttributes($values); - $this->afterSave(true, $values); + $changedAttributes = array_fill_keys(array_keys($values), null); + $this->setOldAttributes($values); + $this->afterSave(true, $changedAttributes); return true; } @@ -530,10 +531,12 @@ abstract class ActiveRecord extends BaseActiveRecord } } + $changedAttributes = []; foreach ($values as $name => $value) { + $changedAttributes[$name] = $this->getOldAttribute($name); $this->setOldAttribute($name, $this->getAttribute($name)); } - $this->afterSave(false, $values); + $this->afterSave(false, $changedAttributes); return $rows; } diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index e67e20df30..4e069d47e3 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -121,6 +121,7 @@ Yii Framework 2 Change Log - Added note about the fact that intl is required for non-latin languages to requirements checker. - Enh #4028: Added ability to `yii\widgets\Menu` to encode each item's label separately (creocoder, umneeq) - Enh #4080: Added proper handling and support of the symlinked directories in `FileHelper`, added $options parameter in `FileHelper::removeDirectory()` (resurtm) +- Enh #4086: changedAttributes of AfterSaveEvent contain an old values (dizews) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue) - Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue) @@ -152,7 +153,6 @@ Yii Framework 2 Change Log - Chg #3956: Flash messages set via `Yii::$app->session->setFlash()` will be removed only if they are accessed (qiangxue) - Chg #3989: The default value for `yii\log\FileTarget::$rotateByCopy` now defaults to true to work on windows by default (cebe) - Chg #4071: `mail` component renamed to `mailer`, `yii\log\EmailTarget::$mail` renamed to `yii\log\EmailTarget::$mailer` (samdark) -- Chg #4086: changedAttributes of AfterSaveEvent contain an old values (dizews) - Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue) - Chg: Added `$user` as the first parameter of `yii\rbac\Rule::execute()` (qiangxue) - Chg: `yii\grid\DataColumn::getDataCellValue()` visibility is now `public` to allow accessing the value from a GridView directly (cebe) diff --git a/framework/UPGRADE.md b/framework/UPGRADE.md index 83fc0fc41c..41e4450847 100644 --- a/framework/UPGRADE.md +++ b/framework/UPGRADE.md @@ -61,6 +61,7 @@ Upgrade from Yii 2.0 Beta * The behavior and signature of `ActiveRecord::afterSave()` has changed. `ActiveRecord::$isNewRecord` will now always be false in afterSave and also dirty attributes are not available. This change has been made to have a more consistent and expected behavior. The changed attributes are now available in the new parameter of afterSave() `$changedAttributes`. + The `$changedAttributes` contain an old values of attributes that had changed and were saved. * `ActiveRecord::updateAttributes()` has been changed to not trigger events and not respect optimistic locking anymore to differentiate it more from calling `update(false)` and to ensure it can be used in `afterSave()` without triggering infinite