db\BaseActiveRecord optimisticLock()

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:

  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. 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.
  4. 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 null is returned (default implemented), optimistic locking will not be supported.

doc_Yii
2016-10-30 16:56:52
Comments
Leave a Comment

Please login to continue.