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.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$owner | yii\ | The owner of this behavior Default: null. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
attach() | Attaches the behavior object to the component. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detach() | Detaches the behavior object from the component. | yii\ |
events() | Declares event handlers for the $owner’s events. | RowVersionBehavior |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
refreshRowVersion() | Re-reads the rowversion optimistic lock attribute from the database into the owner instance. | RowVersionBehavior |
Method details
Section titled “Method details”events()
Section titled “events() ”public function events(): arrayDeclares 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).
refreshRowVersion()
Section titled “refreshRowVersion() ”public function refreshRowVersion(): voidRe-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.