diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 4d9bb1620c..e67e20df30 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -152,6 +152,7 @@ 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/db/ActiveRecord.php b/framework/db/ActiveRecord.php index 8726a2b4b0..51ccaa4113 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -462,8 +462,9 @@ class ActiveRecord extends BaseActiveRecord } } + $changedAttributes = array_fill_keys(array_keys($values), null); $this->setOldAttributes($values); - $this->afterSave(true, $values); + $this->afterSave(true, $changedAttributes); return true; } diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index 076be98908..e61827c156 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -714,10 +714,12 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface throw new StaleObjectException('The object being updated is outdated.'); } + $changedAttributes = []; foreach ($values as $name => $value) { + $changedAttributes[$name] = $this->_oldAttributes[$name]; $this->_oldAttributes[$name] = $this->_attributes[$name]; } - $this->afterSave(false, $values); + $this->afterSave(false, $changedAttributes); return $rows; } @@ -875,7 +877,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface * the event is triggered. * @param boolean $insert whether this method called while inserting 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 attribute values that had changed and were saved contain old values. */ public function afterSave($insert, $changedAttributes) {