Skip to content

Commit

Permalink
allow using existing column to store lock version.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Apr 1, 2013
1 parent c6a1327 commit 2026d38
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions framework/db/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,16 @@ public static function primaryKey()
/**
* 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
* when a user attempts to save the record upon some staled data (because another user
* has modified the data), a [[StaleObjectException]] exception will be thrown, and
* the update or deletion is ignored.
* Optimistic locking allows multiple users to access the same record for edits and avoids
* potential conflicts. In case when a user attempts to save the record upon some staled data
* (because another user has modified the data), a [[StaleObjectException]] exception will be thrown,
* and the update or deletion is skipped.
*
* Optimized locking is only supported by [[update()]] and [[delete()]].
*
* 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.
* 2. In the Web form that collects the user input, add a hidden field that stores
* the lock version of the recording being updated.
Expand Down Expand Up @@ -753,8 +753,10 @@ public function update($runValidation = true, $attributes = null)
$condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock();
if ($lock !== null) {
$values[$lock] = $this->$lock + 1;
$condition[$lock] = new Expression("[[$lock]]+1");
if (!isset($values[$lock])) {
$values[$lock] = $this->$lock + 1;
}
$condition[$lock] = $this->$lock;
}
// 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.
Expand Down

0 comments on commit 2026d38

Please sign in to comment.