Commit 83b0b3d4 by Qiang Xue

Fixes #6311: Optimistic lock for ActiveRecord does not work as expected

parent 39fb85ed
...@@ -41,6 +41,7 @@ Yii Framework 2 Change Log ...@@ -41,6 +41,7 @@ Yii Framework 2 Change Log
- Bug #6271: Query caching returns the same data when running the same SQL with different fetch modes (grachov) - Bug #6271: Query caching returns the same data when running the same SQL with different fetch modes (grachov)
- Bug #6279: `yii\db\Schema::getLastInsertID()` was passing wrong default schema name to PDO (samdark) - Bug #6279: `yii\db\Schema::getLastInsertID()` was passing wrong default schema name to PDO (samdark)
- Bug #6305: `yii\i18n\Formatter::asParagraphs()` was not unicode-aware (samdark) - Bug #6305: `yii\i18n\Formatter::asParagraphs()` was not unicode-aware (samdark)
- Bug #6311: Optimistic lock for ActiveRecord does not work as expected (qiangxue)
- Bug: Gii console command help information does not contain global options (qiangxue) - Bug: Gii console command help information does not contain global options (qiangxue)
- Bug: `yii\web\UrlRule` was unable to create URLs for rules containing unicode characters (samdark) - Bug: `yii\web\UrlRule` was unable to create URLs for rules containing unicode characters (samdark)
- Bug: `yii\web\AssetManager` should not publish disabled asset bundles (qiangxue) - Bug: `yii\web\AssetManager` should not publish disabled asset bundles (qiangxue)
......
...@@ -210,9 +210,10 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -210,9 +210,10 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
* *
* 1. Create a column to store the version number of each row. The column type should be `BIGINT DEFAULT 0`. * 1. Create a column to store the version number of each row. The column type should be `BIGINT DEFAULT 0`.
* Override this method to return the name of this column. * Override this method to return the name of this column.
* 2. In the Web form that collects the user input, add a hidden field that stores * 2. Add a `required` validation rule for the version column to ensure the version value is submitted.
* 3. In the Web form that collects the user input, add a hidden field that stores
* the lock version of the recording being updated. * the lock version of the recording being updated.
* 3. In the controller action that does the data updating, try to catch the [[StaleObjectException]] * 4. In the controller action that does the data updating, try to catch the [[StaleObjectException]]
* and implement necessary business logic (e.g. merging the changes, prompting stated data) * and implement necessary business logic (e.g. merging the changes, prompting stated data)
* to resolve the conflict. * to resolve the conflict.
* *
...@@ -703,9 +704,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface ...@@ -703,9 +704,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
$condition = $this->getOldPrimaryKey(true); $condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock(); $lock = $this->optimisticLock();
if ($lock !== null) { if ($lock !== null) {
if (!isset($values[$lock])) {
$values[$lock] = $this->$lock + 1; $values[$lock] = $this->$lock + 1;
}
$condition[$lock] = $this->$lock; $condition[$lock] = $this->$lock;
} }
// We do not check the return value of updateAll() because it's possible // We do not check the return value of updateAll() because it's possible
......
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