Skip to content
PHP Ecosystem
Explore

RowVersionBehavior

API reference › yii › db › mssql › RowVersionBehavior

Refreshes a server-managed rowversion optimistic lock attribute from the database after each write.

SQL Server regenerates the rowversion value server-side on every INSERT and UPDATE, so the in-memory attribute holds a guessed token afterwards. Attach this behavior to an ActiveRecord whose BaseActiveRecord::optimisticLock() returns a rowversion column to re-read the authoritative token after each write, so repeated saves on the same instance do not raise a spurious StaleObjectException.

Usage example:

public function behaviors(): array
{
return [\yii\db\mssql\RowVersionBehavior::class];
}

See also: https://github.com/yiisoft/yii2/issues/9653.

PropertyTypeDescriptionDefined by
$owneryii\base\T|nullThe owner of this behavior Default: null.yii\base\Behavior
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.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()Attaches the behavior object to the component.yii\base\Behavior
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() staticReturns the fully qualified name of this class.yii\base\BaseObject
detach()Detaches the behavior object from the component.yii\base\Behavior
events()Declares event handlers for the $owner’s events.RowVersionBehavior
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
refreshRowVersion()Re-reads the rowversion optimistic lock attribute from the database into the owner instance.RowVersionBehavior
public function events(): array

Declares event handlers for the $owner’s events.

Child classes may override this method to declare what PHP callbacks should be attached to the events of the $owner component.

The callbacks will be attached to the $owner’s events when the behavior is attached to the owner; and they will be detached from the events when the behavior is detached from the component.

The callbacks can be any of the following:

  • method in this behavior: 'handleClick', equivalent to [$this, 'handleClick']
  • object method: [$object, 'handleClick']
  • static method: ['Page', 'handleClick']
  • anonymous function: function ($event) { ... }

The following is an example:

[
Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
]

Returns

array: Events (array keys) and the corresponding event handler methods (array values).

public function refreshRowVersion(): void

Re-reads the rowversion optimistic lock attribute from the database into the owner instance.

Does nothing when optimistic locking is disabled or the lock column is not a rowversion.