Skip to content

Commit

Permalink
reset dirty attributes after afterSave
Browse files Browse the repository at this point in the history
so information about changed values is available in `afterSave`-event.

fixes #2892
  • Loading branch information
cebe committed Mar 27, 2014
1 parent f7edebb commit f439520
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 27 deletions.
3 changes: 2 additions & 1 deletion extensions/elasticsearch/ActiveRecord.php
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions extensions/elasticsearch/CHANGELOG.md
Expand Up @@ -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`.
Expand Down
8 changes: 3 additions & 5 deletions extensions/mongodb/ActiveRecord.php
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down
8 changes: 3 additions & 5 deletions extensions/mongodb/file/ActiveRecord.php
Expand Up @@ -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;
}
Expand All @@ -167,7 +166,6 @@ protected function updateInternal($attributes = null)
$values = $this->getDirtyAttributes($attributes);
if (empty($values)) {
$this->afterSave(false);

return 0;
}

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/redis/ActiveRecord.php
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions extensions/redis/CHANGELOG.md
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions extensions/sphinx/ActiveRecord.php
Expand Up @@ -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;
}
Expand Down Expand Up @@ -512,7 +511,6 @@ protected function updateInternal($attributes = null)
$values = $this->getDirtyAttributes($attributes);
if (empty($values)) {
$this->afterSave(false);

return 0;
}

Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions extensions/sphinx/CHANGELOG.md
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions framework/db/ActiveRecord.php
Expand Up @@ -450,10 +450,9 @@ protected function insertInternal($attributes = null)
}
}
}
foreach ($values as $name => $value) {
$this->setOldAttribute($name, $value);
}

$this->afterSave(true);
$this->setOldAttributes($values);

This comment has been minimized.

Copy link
@qiangxue

qiangxue Mar 27, 2014

Member

At line 448, there is $this->setOldAttribute($name, $id);. This causes inconsistency.
Also, there is a minor problem to use $this->setOldAttributes($values) because it completely replaces all existing old attributes. I suggest we introduce an extra optional parameter $merge to setOldAttributes().

This comment has been minimized.

Copy link
@cebe

cebe Mar 27, 2014

Author Member

I am using setOldAttributes() on insert as oldAttributes was null before. on update I am doing foreach.
Will fix the inconsistency here.

This comment has been minimized.

Copy link
@qiangxue

qiangxue Mar 27, 2014

Member

Ok.

This comment has been minimized.

Copy link
@cebe

cebe Mar 27, 2014

Author Member

done. c73cfe1


return true;
}
Expand Down
9 changes: 2 additions & 7 deletions framework/db/BaseActiveRecord.php
Expand Up @@ -564,7 +564,6 @@ public function getDirtyAttributes($names = null)
}
}
}

return $attributes;
}

Expand Down Expand Up @@ -654,7 +653,6 @@ public function update($runValidation = true, $attributes = null)
if ($runValidation && !$this->validate($attributes)) {
return false;
}

return $this->updateInternal($attributes);
}

Expand Down Expand Up @@ -684,8 +682,7 @@ public function updateAttributes($attributes)
$attrs[] = $name;
}
}

return $this->update(false, $attrs);
return $this->updateInternal($attrs);
}

/**
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -751,7 +747,6 @@ public function updateCounters($counters)
$this->_attributes[$name] += $value;
$this->_oldAttributes[$name] = $this->_attributes[$name];
}

return true;
} else {
return false;
Expand Down

1 comment on commit f439520

@qiangxue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is broken.

Please sign in to comment.