From 51a442d6c8266bf14e258cc77625b6d192f678d0 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Tue, 26 May 2015 11:53:34 +0300 Subject: [PATCH] Fixed `yii\db\ActiveRecord` does not updates attribute specified at `optimisticLock()` after save --- framework/CHANGELOG.md | 1 + framework/db/BaseActiveRecord.php | 3 +++ tests/data/ar/Document.php | 17 +++++++++++++++++ tests/data/cubrid.sql | 11 +++++++++++ tests/data/mssql.sql | 13 +++++++++++++ tests/data/mysql.sql | 11 +++++++++++ tests/data/oci.sql | 13 +++++++++++++ tests/data/postgres.sql | 11 ++++++++++- tests/data/sqlite.sql | 11 +++++++++++ tests/framework/db/ActiveRecordTest.php | 17 +++++++++++++++++ 10 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 tests/data/ar/Document.php diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 6055f546cf..1fa0896147 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -11,6 +11,7 @@ Yii Framework 2 Change Log - Bug #8451: `yii\i18n\Formatter` did not allow negative unix timestamps as input for date formatting (cebe) - Bug #8483: sequence name in `Schema::getLastInsertId()` was not properly quoted (nineinchnick) - Bug #8506: Cleaning of output buffer in `Widget::run()` conflicts with `Pjax` widget which did the cleanup itself (cebe, joester89) +- Bug #8544: Fixed `yii\db\ActiveRecord` does not updates attribute specified at `optimisticLock()` after save (klimov-paul) - Bug: Fixed string comparison in `BaseActiveRecord::unlink()` which may result in wrong comparison result for hash valued primary keys starting with `0e` (cebe) - Enh #7169: `yii\widgets\ActiveField` now uses corresponding methods for default parts rendering (klimov-paul) - Enh #8070: `yii\console\controllers\MessageController` now sorts created messages, even if there is no new one, while saving to PHP file (klimov-paul) diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index d99192a05e..a479819d9b 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -725,6 +725,9 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface foreach ($values as $name => $value) { $changedAttributes[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null; $this->_oldAttributes[$name] = $value; + if ($name === $lock) { + $this->$lock = $value; + } } $this->afterSave(false, $changedAttributes); diff --git a/tests/data/ar/Document.php b/tests/data/ar/Document.php new file mode 100644 index 0000000000..e4b7bc857a --- /dev/null +++ b/tests/data/ar/Document.php @@ -0,0 +1,17 @@ +assertTrue($record->save(false)); $this->assertEquals(1, $record->id); } + + public function testOptimisticLock() + { + /* @var $record Document */ + + $record = Document::findOne(1); + $record->content = 'New Content'; + $record->save(false); + $this->assertEquals(1, $record->version); + + $record = Document::findOne(1); + $record->content = 'Rewrite attempt content'; + $record->version = 0; + $this->setExpectedException('yii\db\StaleObjectException'); + $record->save(false); + } }