OptimisticLockBehavior.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\behaviors;
  8. use Yii;
  9. use yii\db\BaseActiveRecord;
  10. use yii\base\InvalidCallException;
  11. use yii\validators\NumberValidator;
  12. use yii\helpers\ArrayHelper;
  13. /**
  14. * OptimisticLockBehavior automatically upgrades a model's lock version using the column name
  15. * returned by [[\yii\db\BaseActiveRecord::optimisticLock()|optimisticLock()]].
  16. *
  17. * Optimistic locking allows multiple users to access the same record for edits and avoids
  18. * potential conflicts. In case when a user attempts to save the record upon some staled data
  19. * (because another user has modified the data), a [[StaleObjectException]] exception will be thrown,
  20. * and the update or deletion is skipped.
  21. *
  22. * To use this behavior, first enable optimistic lock by following the steps listed in
  23. * [[\yii\db\BaseActiveRecord::optimisticLock()|optimisticLock()]], remove the column name
  24. * holding the lock version from the [[\yii\base\Model::rules()|rules()]] method of your
  25. * ActiveRecord class, then add the following code to it:
  26. *
  27. * ```php
  28. * use yii\behaviors\OptimisticLockBehavior;
  29. *
  30. * public function behaviors()
  31. * {
  32. * return [
  33. * OptimisticLockBehavior::className(),
  34. * ];
  35. * }
  36. * ```
  37. *
  38. * By default, OptimisticLockBehavior will use [[\yii\web\Request::getBodyParam()|getBodyParam()]] to parse
  39. * the submitted value or set it to 0 on any fail. That means a request not holding the version attribute
  40. * may achieve a first successful update to entity, but starting from there any further try should fail
  41. * unless the request is holding the expected version number.
  42. *
  43. * Once attached, internal use of the model class should also fail to save the record if the version number
  44. * isn't held by [[\yii\web\Request::getBodyParam()|getBodyParam()]]. It may be useful to extend your model class,
  45. * enable optimistic lock in parent class by overriding [[\yii\db\BaseActiveRecord::optimisticLock()|optimisticLock()]],
  46. * then attach the behavior to the child class so you can tie the parent model to internal use while linking the child model
  47. * holding this behavior to the controllers responsible of receiving end user inputs.
  48. * Alternatively, you can also configure the [[value]] property with a PHP callable to implement a different logic.
  49. *
  50. * OptimisticLockBehavior also provides a method named [[upgrade()]] that increases a model's
  51. * version by one, that may be useful when you need to mark an entity as stale among connected clients
  52. * and avoid any change to it until they load it again:
  53. *
  54. * ```php
  55. * $model->upgrade();
  56. * ```
  57. *
  58. * @author Salem Ouerdani <tunecino@gmail.com>
  59. * @since 2.0.16
  60. * @see \yii\db\BaseActiveRecord::optimisticLock() for details on how to enable optimistic lock.
  61. */
  62. class OptimisticLockBehavior extends AttributeBehavior
  63. {
  64. /**
  65. * {@inheritdoc}
  66. *
  67. * In case of `null` value it will be directly parsed from [[\yii\web\Request::getBodyParam()|getBodyParam()]] or set to 0.
  68. */
  69. public $value;
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public $skipUpdateOnClean = false;
  74. /**
  75. * @var string the attribute name holding the version value.
  76. */
  77. private $_lockAttribute;
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function attach($owner)
  82. {
  83. parent::attach($owner);
  84. if (empty($this->attributes)) {
  85. $lock = $this->getLockAttribute();
  86. $this->attributes = array_fill_keys(array_keys($this->events()), $lock);
  87. }
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function events()
  93. {
  94. return Yii::$app->request instanceof \yii\web\Request ? [
  95. BaseActiveRecord::EVENT_BEFORE_INSERT => 'evaluateAttributes',
  96. BaseActiveRecord::EVENT_BEFORE_UPDATE => 'evaluateAttributes',
  97. BaseActiveRecord::EVENT_BEFORE_DELETE => 'evaluateAttributes',
  98. ] : [];
  99. }
  100. /**
  101. * Returns the column name to hold the version value as defined in [[\yii\db\BaseActiveRecord::optimisticLock()|optimisticLock()]].
  102. * @return string the property name.
  103. * @throws InvalidCallException if [[\yii\db\BaseActiveRecord::optimisticLock()|optimisticLock()]] is not properly configured.
  104. * @since 2.0.16
  105. */
  106. protected function getLockAttribute()
  107. {
  108. if ($this->_lockAttribute) {
  109. return $this->_lockAttribute;
  110. }
  111. /* @var $owner BaseActiveRecord */
  112. $owner = $this->owner;
  113. $lock = $owner->optimisticLock();
  114. if ($lock === null || $owner->hasAttribute($lock) === false) {
  115. throw new InvalidCallException("Unable to get the optimistic lock attribute. Probably 'optimisticLock()' method is misconfigured.");
  116. }
  117. $this->_lockAttribute = $lock;
  118. return $lock;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. *
  123. * In case of `null`, value will be parsed from [[\yii\web\Request::getBodyParam()|getBodyParam()]] or set to 0.
  124. */
  125. protected function getValue($event)
  126. {
  127. if ($this->value === null) {
  128. $request = Yii::$app->getRequest();
  129. $lock = $this->getLockAttribute();
  130. $formName = $this->owner->formName();
  131. $formValue = $formName ? ArrayHelper::getValue($request->getBodyParams(), $formName . '.' . $lock) : null;
  132. $input = $formValue ?: $request->getBodyParam($lock);
  133. $isValid = $input && (new NumberValidator())->validate($input);
  134. return $isValid ? $input : 0;
  135. }
  136. return parent::getValue($event);
  137. }
  138. /**
  139. * Upgrades the version value by one and stores it to database.
  140. *
  141. * ```php
  142. * $model->upgrade();
  143. * ```
  144. * @throws InvalidCallException if owner is a new record.
  145. * @since 2.0.16
  146. */
  147. public function upgrade()
  148. {
  149. /* @var $owner BaseActiveRecord */
  150. $owner = $this->owner;
  151. if ($owner->getIsNewRecord()) {
  152. throw new InvalidCallException('Upgrading the model version is not possible on a new record.');
  153. }
  154. $lock = $this->getLockAttribute();
  155. $version = $owner->$lock ?: 0;
  156. $owner->updateAttributes([$lock => $version + 1]);
  157. }
  158. }