Commit 2d2c9f03 by Carsten Brandt

Merge pull request #4092 from dizews/afterUpdate_event_ench

changedAttributes of AfterSaveEvent contain an old values
parents c484536b f9febd00
...@@ -416,8 +416,9 @@ class ActiveRecord extends BaseActiveRecord ...@@ -416,8 +416,9 @@ class ActiveRecord extends BaseActiveRecord
$this->_version = $response['_version']; $this->_version = $response['_version'];
$this->_score = null; $this->_score = null;
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values); $this->setOldAttributes($values);
$this->afterSave(true, $values); $this->afterSave(true, $changedAttributes);
return true; return true;
} }
......
...@@ -224,8 +224,9 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -224,8 +224,9 @@ abstract class ActiveRecord extends BaseActiveRecord
$this->setAttribute('_id', $newId); $this->setAttribute('_id', $newId);
$values['_id'] = $newId; $values['_id'] = $newId;
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values); $this->setOldAttributes($values);
$this->afterSave(true, $values); $this->afterSave(true, $changedAttributes);
return true; return true;
} }
...@@ -260,10 +261,12 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -260,10 +261,12 @@ abstract class ActiveRecord extends BaseActiveRecord
throw new StaleObjectException('The object being updated is outdated.'); throw new StaleObjectException('The object being updated is outdated.');
} }
$changedAttributes = [];
foreach ($values as $name => $value) { foreach ($values as $name => $value) {
$changedAttributes[$name] = $this->getOldAttribute($name);
$this->setOldAttribute($name, $this->getAttribute($name)); $this->setOldAttribute($name, $this->getAttribute($name));
} }
$this->afterSave(false, $values); $this->afterSave(false, $changedAttributes);
return $rows; return $rows;
} }
......
...@@ -127,8 +127,9 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord ...@@ -127,8 +127,9 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord
$this->setAttribute('_id', $newId); $this->setAttribute('_id', $newId);
$values['_id'] = $newId; $values['_id'] = $newId;
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values); $this->setOldAttributes($values);
$this->afterSave(true, $values); $this->afterSave(true, $changedAttributes);
return true; return true;
} }
...@@ -196,10 +197,12 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord ...@@ -196,10 +197,12 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord
} }
} }
$changedAttributes = [];
foreach ($values as $name => $value) { foreach ($values as $name => $value) {
$changedAttributes[$name] = $this->getOldAttribute($name);
$this->setOldAttribute($name, $this->getAttribute($name)); $this->setOldAttribute($name, $this->getAttribute($name));
} }
$this->afterSave(false, $values); $this->afterSave(false, $changedAttributes);
return $rows; return $rows;
} }
......
...@@ -124,8 +124,9 @@ class ActiveRecord extends BaseActiveRecord ...@@ -124,8 +124,9 @@ class ActiveRecord extends BaseActiveRecord
} }
$db->executeCommand('HMSET', $args); $db->executeCommand('HMSET', $args);
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values); $this->setOldAttributes($values);
$this->afterSave(true, $values); $this->afterSave(true, $changedAttributes);
return true; return true;
} }
......
...@@ -395,8 +395,9 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -395,8 +395,9 @@ abstract class ActiveRecord extends BaseActiveRecord
return false; return false;
} }
$this->setOldAttributes($values); $changedAttributes = array_fill_keys(array_keys($values), null);
$this->afterSave(true, $values); $this->setOldAttributes($values);
$this->afterSave(true, $changedAttributes);
return true; return true;
} }
...@@ -530,10 +531,12 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -530,10 +531,12 @@ abstract class ActiveRecord extends BaseActiveRecord
} }
} }
$changedAttributes = [];
foreach ($values as $name => $value) { foreach ($values as $name => $value) {
$changedAttributes[$name] = $this->getOldAttribute($name);
$this->setOldAttribute($name, $this->getAttribute($name)); $this->setOldAttribute($name, $this->getAttribute($name));
} }
$this->afterSave(false, $values); $this->afterSave(false, $changedAttributes);
return $rows; return $rows;
} }
......
...@@ -122,6 +122,7 @@ Yii Framework 2 Change Log ...@@ -122,6 +122,7 @@ Yii Framework 2 Change Log
- Added note about the fact that intl is required for non-latin languages to requirements checker. - 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 #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 #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: 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: 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) - Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue)
......
...@@ -61,6 +61,7 @@ Upgrade from Yii 2.0 Beta ...@@ -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 * 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 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`. 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 * `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 differentiate it more from calling `update(false)` and to ensure it can be used in `afterSave()` without triggering infinite
...@@ -93,4 +94,4 @@ Upgrade from Yii 2.0 Beta ...@@ -93,4 +94,4 @@ Upgrade from Yii 2.0 Beta
], ],
// ... // ...
]; ];
``` ```
\ No newline at end of file
...@@ -462,8 +462,9 @@ class ActiveRecord extends BaseActiveRecord ...@@ -462,8 +462,9 @@ class ActiveRecord extends BaseActiveRecord
} }
} }
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values); $this->setOldAttributes($values);
$this->afterSave(true, $values); $this->afterSave(true, $changedAttributes);
return true; return true;
} }
......
...@@ -714,10 +714,12 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -714,10 +714,12 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
throw new StaleObjectException('The object being updated is outdated.'); throw new StaleObjectException('The object being updated is outdated.');
} }
$changedAttributes = [];
foreach ($values as $name => $value) { foreach ($values as $name => $value) {
$changedAttributes[$name] = $this->_oldAttributes[$name];
$this->_oldAttributes[$name] = $this->_attributes[$name]; $this->_oldAttributes[$name] = $this->_attributes[$name];
} }
$this->afterSave(false, $values); $this->afterSave(false, $changedAttributes);
return $rows; return $rows;
} }
...@@ -875,7 +877,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -875,7 +877,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
* the event is triggered. * the event is triggered.
* @param boolean $insert whether this method called while inserting a record. * @param boolean $insert whether this method called while inserting a record.
* If false, it means the method is called while updating a record. * If false, it means the method is called while updating a record.
* @param array $changedAttributes The attribute values that had changed and were saved. * @param array $changedAttributes The old values of attributes that had changed and were saved.
*/ */
public function afterSave($insert, $changedAttributes) public function afterSave($insert, $changedAttributes)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment