Class yii\behaviors\OptimisticLockBehavior
Inheritance | yii\behaviors\OptimisticLockBehavior » yii\behaviors\AttributeBehavior » yii\base\Behavior » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.0.16 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/behaviors/OptimisticLockBehavior.php |
OptimisticLockBehavior automatically upgrades a model's lock version using the column name returned by optimisticLock().
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\behaviors\StaleObjectException exception will be thrown, and the update or deletion is skipped.
To use this behavior, first enable optimistic lock by following the steps listed in optimisticLock(), remove the column name holding the lock version from the rules() method of your ActiveRecord class, then add the following code to it:
use yii\behaviors\OptimisticLockBehavior;
public function behaviors()
{
return [
OptimisticLockBehavior::className(),
];
}
By default, OptimisticLockBehavior will use getBodyParam() to parse the submitted value or set it to 0 on any fail. That means a request not holding the version attribute may achieve a first successful update to entity, but starting from there any further try should fail unless the request is holding the expected version number.
Once attached, internal use of the model class should also fail to save the record if the version number isn't held by getBodyParam(). It may be useful to extend your model class, enable optimistic lock in parent class by overriding optimisticLock(), then attach the behavior to the child class so you can tie the parent model to internal use while linking the child model holding this behavior to the controllers responsible of receiving end user inputs. Alternatively, you can also configure the $value property with a PHP callable to implement a different logic.
OptimisticLockBehavior also provides a method named upgrade() that increases a model's version by one, that may be useful when you need to mark an entity as stale among connected clients and avoid any change to it until they load it again:
$model->upgrade();
See also yii\db\BaseActiveRecord::optimisticLock() for details on how to enable optimistic lock.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$attributes | array | List of attributes that are to be automatically filled with the value specified via $value. | yii\behaviors\AttributeBehavior |
$owner | yii\base\Component|null | The owner of this behavior | yii\base\Behavior |
$preserveNonEmptyValues | boolean | Whether to preserve non-empty attribute values. | yii\behaviors\AttributeBehavior |
$skipUpdateOnClean | {@inheritdoc} | yii\behaviors\OptimisticLockBehavior | |
$value | {@inheritdoc} | yii\behaviors\OptimisticLockBehavior |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\BaseObject |
__construct() | Constructor. | yii\base\BaseObject |
__get() | Returns the value of an object property. | yii\base\BaseObject |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
__set() | Sets value of an object property. | yii\base\BaseObject |
__unset() | Sets an object property to null. | yii\base\BaseObject |
attach() | {@inheritdoc} | yii\behaviors\OptimisticLockBehavior |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
detach() | Detaches the behavior object from the component. | yii\base\Behavior |
evaluateAttributes() | Evaluates the attribute value and assigns it to the current attributes. | yii\behaviors\AttributeBehavior |
events() | {@inheritdoc} | yii\behaviors\OptimisticLockBehavior |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\BaseObject |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject |
init() | Initializes the object. | yii\base\BaseObject |
upgrade() | Upgrades the version value by one and stores it to database. | yii\behaviors\OptimisticLockBehavior |
Protected Methods
Method | Description | Defined By |
---|---|---|
getLockAttribute() | Returns the column name to hold the version value as defined in optimisticLock(). | yii\behaviors\OptimisticLockBehavior |
getValue() | {@inheritdoc} | yii\behaviors\OptimisticLockBehavior |
Property Details
In case of null
value it will be directly parsed from getBodyParam() or set to 0.
Method Details
{@inheritdoc}
public void attach ( $owner ) | ||
$owner |
{@inheritdoc}
public void events ( ) |
Returns the column name to hold the version value as defined in optimisticLock().
protected string getLockAttribute ( ) | ||
return | string | The property name. |
---|---|---|
throws | yii\base\InvalidCallException | if optimisticLock() is not properly configured. |
{@inheritdoc}
In case of null
, value will be parsed from getBodyParam() or set to 0.
protected void getValue ( $event ) | ||
$event |
Upgrades the version value by one and stores it to database.
$model->upgrade();
public void upgrade ( ) | ||
throws | yii\base\InvalidCallException | if owner is a new record. |
---|