optimisticLock() public method
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 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 yii\db\StaleObjectException exception will be thrown, and the update or deletion is skipped.
Optimistic locking is only supported by update() and delete().
To use Optimistic locking:
- 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. - Add a
required
validation rule for the version column to ensure the version value is submitted. - In the Web form that collects the user input, add a hidden field that stores the lock version of the recording being updated.
- In the controller action that does the data updating, try to catch the yii\db\StaleObjectException and implement necessary business logic (e.g. merging the changes, prompting stated data) to resolve the conflict.
public string optimisticLock ( ) | ||
---|---|---|
return | string |
The column name that stores the lock version of a table row. If |
Please login to continue.