Commit 2026d382 by Qiang Xue

allow using existing column to store lock version.

parent c6a13278
...@@ -279,16 +279,16 @@ class ActiveRecord extends Model ...@@ -279,16 +279,16 @@ class ActiveRecord extends Model
/** /**
* Returns the name of the column that stores the lock version for implementing optimistic locking. * Returns the name of the column that stores the lock version for implementing optimistic locking.
* *
* Optimistic locking allows multiple users to access the same record for edits. In case * Optimistic locking allows multiple users to access the same record for edits and avoids
* when a user attempts to save the record upon some staled data (because another user * potential conflicts. In case when a user attempts to save the record upon some staled data
* has modified the data), a [[StaleObjectException]] exception will be thrown, and * (because another user has modified the data), a [[StaleObjectException]] exception will be thrown,
* the update or deletion is ignored. * and the update or deletion is skipped.
* *
* Optimized locking is only supported by [[update()]] and [[delete()]]. * Optimized locking is only supported by [[update()]] and [[delete()]].
* *
* To use optimized locking: * To use optimized locking:
* *
* 1. create a column to store the lock version. 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. 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.
...@@ -753,8 +753,10 @@ class ActiveRecord extends Model ...@@ -753,8 +753,10 @@ class ActiveRecord extends Model
$condition = $this->getOldPrimaryKey(true); $condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock(); $lock = $this->optimisticLock();
if ($lock !== null) { if ($lock !== null) {
$values[$lock] = $this->$lock + 1; if (!isset($values[$lock])) {
$condition[$lock] = new Expression("[[$lock]]+1"); $values[$lock] = $this->$lock + 1;
}
$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
// that the UPDATE statement doesn't change anything and thus returns 0. // that the UPDATE statement doesn't change anything and thus returns 0.
......
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