Commit f4395206 by Carsten Brandt

reset dirty attributes after `afterSave`

so information about changed values is available in `afterSave`-event. fixes #2892
parent f7edebb2
...@@ -395,8 +395,9 @@ class ActiveRecord extends BaseActiveRecord ...@@ -395,8 +395,9 @@ class ActiveRecord extends BaseActiveRecord
} }
$this->_version = $response['_version']; $this->_version = $response['_version'];
$this->_score = null; $this->_score = null;
$this->setOldAttributes($values);
$this->afterSave(true); $this->afterSave(true);
$this->setOldAttributes($values);
return true; return true;
} }
......
...@@ -9,6 +9,7 @@ Yii Framework 2 elasticsearch extension Change Log ...@@ -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 #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 #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 #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 #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 #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`. - Chg #2146: Removed `ActiveRelation` class and moved the functionality to `ActiveQuery`.
......
...@@ -242,10 +242,9 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -242,10 +242,9 @@ abstract class ActiveRecord extends BaseActiveRecord
} }
$newId = static::getCollection()->insert($values); $newId = static::getCollection()->insert($values);
$this->setAttribute('_id', $newId); $this->setAttribute('_id', $newId);
foreach ($values as $name => $value) {
$this->setOldAttribute($name, $value);
}
$this->afterSave(true); $this->afterSave(true);
$this->setOldAttributes($values);
return true; return true;
} }
...@@ -262,7 +261,6 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -262,7 +261,6 @@ abstract class ActiveRecord extends BaseActiveRecord
$values = $this->getDirtyAttributes($attributes); $values = $this->getDirtyAttributes($attributes);
if (empty($values)) { if (empty($values)) {
$this->afterSave(false); $this->afterSave(false);
return 0; return 0;
} }
$condition = $this->getOldPrimaryKey(true); $condition = $this->getOldPrimaryKey(true);
...@@ -281,10 +279,10 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -281,10 +279,10 @@ abstract class ActiveRecord extends BaseActiveRecord
throw new StaleObjectException('The object being updated is outdated.'); throw new StaleObjectException('The object being updated is outdated.');
} }
$this->afterSave(false);
foreach ($values as $name => $value) { foreach ($values as $name => $value) {
$this->setOldAttribute($name, $this->getAttribute($name)); $this->setOldAttribute($name, $this->getAttribute($name));
} }
$this->afterSave(false);
return $rows; return $rows;
} }
......
...@@ -147,10 +147,9 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord ...@@ -147,10 +147,9 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord
$newId = $collection->insert($values); $newId = $collection->insert($values);
} }
$this->setAttribute('_id', $newId); $this->setAttribute('_id', $newId);
foreach ($values as $name => $value) {
$this->setOldAttribute($name, $value);
}
$this->afterSave(true); $this->afterSave(true);
$this->setOldAttributes($values);
return true; return true;
} }
...@@ -167,7 +166,6 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord ...@@ -167,7 +166,6 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord
$values = $this->getDirtyAttributes($attributes); $values = $this->getDirtyAttributes($attributes);
if (empty($values)) { if (empty($values)) {
$this->afterSave(false); $this->afterSave(false);
return 0; return 0;
} }
...@@ -219,10 +217,10 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord ...@@ -219,10 +217,10 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord
} }
} }
$this->afterSave(false);
foreach ($values as $name => $value) { foreach ($values as $name => $value) {
$this->setOldAttribute($name, $this->getAttribute($name)); $this->setOldAttribute($name, $this->getAttribute($name));
} }
$this->afterSave(false);
return $rows; return $rows;
} }
......
...@@ -146,8 +146,8 @@ class ActiveRecord extends BaseActiveRecord ...@@ -146,8 +146,8 @@ class ActiveRecord extends BaseActiveRecord
} }
$db->executeCommand('HMSET', $args); $db->executeCommand('HMSET', $args);
$this->setOldAttributes($values);
$this->afterSave(true); $this->afterSave(true);
$this->setOldAttributes($values);
return true; return true;
} }
......
...@@ -6,6 +6,7 @@ Yii Framework 2 redis extension Change Log ...@@ -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) - 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 #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 #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`. - Chg #2146: Removed `ActiveRelation` class and moved the functionality to `ActiveQuery`.
All relational queries are now directly served by `ActiveQuery` allowing to use All relational queries are now directly served by `ActiveQuery` allowing to use
......
...@@ -416,10 +416,9 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -416,10 +416,9 @@ abstract class ActiveRecord extends BaseActiveRecord
if (!$command->execute()) { if (!$command->execute()) {
return false; return false;
} }
foreach ($values as $name => $value) {
$this->setOldAttribute($name, $value);
}
$this->afterSave(true); $this->afterSave(true);
$this->setOldAttributes($values);
return true; return true;
} }
...@@ -512,7 +511,6 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -512,7 +511,6 @@ abstract class ActiveRecord extends BaseActiveRecord
$values = $this->getDirtyAttributes($attributes); $values = $this->getDirtyAttributes($attributes);
if (empty($values)) { if (empty($values)) {
$this->afterSave(false); $this->afterSave(false);
return 0; return 0;
} }
...@@ -554,10 +552,10 @@ abstract class ActiveRecord extends BaseActiveRecord ...@@ -554,10 +552,10 @@ abstract class ActiveRecord extends BaseActiveRecord
} }
} }
$this->afterSave(false);
foreach ($values as $name => $value) { foreach ($values as $name => $value) {
$this->setOldAttribute($name, $this->getAttribute($name)); $this->setOldAttribute($name, $this->getAttribute($name));
} }
$this->afterSave(false);
return $rows; return $rows;
} }
......
...@@ -7,6 +7,7 @@ Yii Framework 2 sphinx extension Change Log ...@@ -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 #1993: afterFind event in AR is now called after relations have been populated (cebe, creocoder)
- Bug #2160: SphinxQL does not support `OFFSET` (qiangxue, romeo7) - Bug #2160: SphinxQL does not support `OFFSET` (qiangxue, romeo7)
- Enh #1398: Refactor ActiveRecord to use BaseActiveRecord class of the framework (klimov-paul) - 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 #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`. - Chg #2146: Removed `ActiveRelation` class and moved the functionality to `ActiveQuery`.
All relational queries are now directly served by `ActiveQuery` allowing to use All relational queries are now directly served by `ActiveQuery` allowing to use
......
...@@ -156,6 +156,7 @@ Yii Framework 2 Change Log ...@@ -156,6 +156,7 @@ Yii Framework 2 Change Log
- Enh #2735: Added support for `DateTimeInterface` in `Formatter` (ivokund) - Enh #2735: Added support for `DateTimeInterface` in `Formatter` (ivokund)
- Enh #2756: Added support for injecting custom `isEmpty` check for all validators (qiangxue) - 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 #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 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 `favicon.ico` and `robots.txt` to default application templates (samdark)
- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue) - Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)
......
...@@ -450,10 +450,9 @@ class ActiveRecord extends BaseActiveRecord ...@@ -450,10 +450,9 @@ class ActiveRecord extends BaseActiveRecord
} }
} }
} }
foreach ($values as $name => $value) {
$this->setOldAttribute($name, $value);
}
$this->afterSave(true); $this->afterSave(true);
$this->setOldAttributes($values);
return true; return true;
} }
......
...@@ -564,7 +564,6 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -564,7 +564,6 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
} }
} }
} }
return $attributes; return $attributes;
} }
...@@ -654,7 +653,6 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -654,7 +653,6 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
if ($runValidation && !$this->validate($attributes)) { if ($runValidation && !$this->validate($attributes)) {
return false; return false;
} }
return $this->updateInternal($attributes); return $this->updateInternal($attributes);
} }
...@@ -684,8 +682,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -684,8 +682,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
$attrs[] = $name; $attrs[] = $name;
} }
} }
return $this->updateInternal($attrs);
return $this->update(false, $attrs);
} }
/** /**
...@@ -700,7 +697,6 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -700,7 +697,6 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
$values = $this->getDirtyAttributes($attributes); $values = $this->getDirtyAttributes($attributes);
if (empty($values)) { if (empty($values)) {
$this->afterSave(false); $this->afterSave(false);
return 0; return 0;
} }
$condition = $this->getOldPrimaryKey(true); $condition = $this->getOldPrimaryKey(true);
...@@ -719,10 +715,10 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -719,10 +715,10 @@ 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.');
} }
$this->afterSave(false);
foreach ($values as $name => $value) { foreach ($values as $name => $value) {
$this->_oldAttributes[$name] = $this->_attributes[$name]; $this->_oldAttributes[$name] = $this->_attributes[$name];
} }
$this->afterSave(false);
return $rows; return $rows;
} }
...@@ -751,7 +747,6 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -751,7 +747,6 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
$this->_attributes[$name] += $value; $this->_attributes[$name] += $value;
$this->_oldAttributes[$name] = $this->_attributes[$name]; $this->_oldAttributes[$name] = $this->_attributes[$name];
} }
return true; return true;
} else { } else {
return false; return false;
......
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