From 51a442d6c8266bf14e258cc77625b6d192f678d0 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Tue, 26 May 2015 11:53:34 +0300 Subject: [PATCH 1/4] 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); + } } From 467596633d3b3dd271d0213fbbffba41e3293982 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Tue, 26 May 2015 12:00:28 +0300 Subject: [PATCH 2/4] Code optimization --- framework/db/BaseActiveRecord.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/framework/db/BaseActiveRecord.php b/framework/db/BaseActiveRecord.php index a479819d9b..a99fdd510d 100644 --- a/framework/db/BaseActiveRecord.php +++ b/framework/db/BaseActiveRecord.php @@ -721,13 +721,14 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface throw new StaleObjectException('The object being updated is outdated.'); } + if (isset($values[$lock])) { + $this->$lock = $values[$lock]; + } + $changedAttributes = []; 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); From 1fbe575feff28fd5b611b30eb5784de3024e8233 Mon Sep 17 00:00:00 2001 From: Nobuo Kihara Date: Tue, 26 May 2015 18:50:07 +0900 Subject: [PATCH 3/4] docs/internals-ja updated [ci skip] --- docs/internals-ja/design-decisions.md | 2 +- docs/internals-ja/translation-workflow.md | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/internals-ja/design-decisions.md b/docs/internals-ja/design-decisions.md index 862eb9afdb..90b95faa98 100644 --- a/docs/internals-ja/design-decisions.md +++ b/docs/internals-ja/design-decisions.md @@ -15,7 +15,7 @@ 3. **[認証クライアントのサポートの追加](https://github.com/yiisoft/yii2/issues/1652)** 保守性を高めるために、コアエクステンションには認証クライアントをこれ以上追加しない。 認証クライアントの追加はユーザエクステンションの形でなされるべきである。 -4. **クロージャを使うときは**、たとえ使用されないものがある場合でも、**渡されたすべてのパラメータをシグニチャに含める** ことが推奨される。 +4. **クロージャを使うときは**、たとえ使用されないものがある場合でも、**渡されるすべてのパラメータをシグニチャに含める** ことが推奨される。 このようにすると、全ての情報が直接に見えるので、コードの修正やコピーがより容易になり、どのパラメータが実際に利用できるかをドキュメントで調べる必要がなくなる。 ([#6584](https://github.com/yiisoft/yii2/pull/6584), [#6875](https://github.com/yiisoft/yii2/issues/6875)) 5. データベーススキーマでは **unsigned int より int** を使う。 diff --git a/docs/internals-ja/translation-workflow.md b/docs/internals-ja/translation-workflow.md index f37d31553c..24b3e0f8bd 100644 --- a/docs/internals-ja/translation-workflow.md +++ b/docs/internals-ja/translation-workflow.md @@ -12,12 +12,12 @@ Yii は国際的なアプリケーションと開発者にとって役に立つ メッセージの翻訳を開始するためには: -1. `framework/messages/config.php` をチェックして、あなたの言語が `languages` のリストに挙っていることを確認してください。 - もし挙っていなければ、あなたの言語をそこに追加します (リストをアルファベット順に保つことを忘れないでください)。 +1. `framework/messages/config.php` をチェックして、あなたの言語が `languages` のリストに載っていることを確認してください。 + もし無ければ、あなたの言語をそこに追加します (リストをアルファベット順に保つことを忘れないでください)。 言語コードの形式は、例えば `ru` や `zh-CN` のように、[IETF言語タグ](http://ja.wikipedia.org/wiki/IETF%E8%A8%80%E8%AA%9E%E3%82%BF%E3%82%B0) に従うべきです。 2. `framework` に入って、`yii message/extract messages/config.php` を走らせます。 3. `framework/messages/your_language/yii.php` のメッセージを翻訳します。ファイルは必ず UTF-8 エンコーディングを使って保存してください。 -4. [プルリクエスト](https://github.com/yiisoft/yii2/blob/master/docs/internals/git-workflow.md) をします。 +4. [プルリクエスト](git-workflow.md) をします。 あなたの翻訳を最新状態に保つために、`yii message/extract messages/config.php` を再び走らせることが出来ます。 このコマンドは、変更のなかった箇所には触れることなく、自動的にメッセージを再抽出してくれます。 From b2e8bf255218eeb2da0cc4d8bfe3a9ceb1b653fe Mon Sep 17 00:00:00 2001 From: Mohamad Mohebifar Date: Tue, 26 May 2015 15:47:11 +0430 Subject: [PATCH 4/4] Update PHPDoc Comment --- framework/base/Widget.php | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/base/Widget.php b/framework/base/Widget.php index 76a08951af..742fb2193a 100644 --- a/framework/base/Widget.php +++ b/framework/base/Widget.php @@ -85,6 +85,7 @@ class Widget extends Component implements ViewContextInterface * The widget rendering result is returned by this method. * @param array $config name-value pairs that will be used to initialize the object properties * @return string the rendering result of the widget. + * @throws \Exception */ public static function widget($config = []) {