OptimisticLockBehavior.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. * Once attached, internal use of the model class should also fail to save the record if the version number
  43. * isn't held by [[\yii\web\Request::getBodyParam()|getBodyParam()]]. It may be useful to extend your model class,
  44. * enable optimistic lock in parent class by overriding [[\yii\db\BaseActiveRecord::optimisticLock()|optimisticLock()]],
  45. * then attach the behavior to the child class so you can tie the parent model to internal use while linking the child model
  46. * holding this behavior to the controllers responsible of receiving end user inputs.
  47. * Alternatively, you can also configure the [[value]] property with a PHP callable to implement a different logic.
  48. *
  49. * OptimisticLockBehavior also provides a method named [[upgrade()]] that increases a model's
  50. * version by one, that may be useful when you need to mark an entity as stale among connected clients
  51. * and avoid any change to it until they load it again:
  52. *
  53. * ```php
  54. * $model->upgrade();
  55. * ```
  56. *
  57. * @author Salem Ouerdani <tunecino@gmail.com>
  58. * @since 2.0.16
  59. * @see \yii\db\BaseActiveRecord::optimisticLock() for details on how to enable optimistic lock.
  60. */
  61. class OptimisticLockBehavior extends AttributeBehavior
  62. {
  63. /**
  64. * {@inheritdoc}
  65. *
  66. * In case of `null` value it will be directly parsed from [[\yii\web\Request::getBodyParam()|getBodyParam()]] or set to 0.
  67. */
  68. public $value;
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public $skipUpdateOnClean = false;
  73. /**
  74. * @var string the attribute name holding the version value.
  75. */
  76. private $_lockAttribute;
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function attach($owner)
  81. {
  82. parent::attach($owner);
  83. if (empty($this->attributes)) {
  84. $lock = $this->getLockAttribute();
  85. $this->attributes = array_fill_keys(array_keys($this->events()), $lock);
  86. }
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function events()
  92. {
  93. return Yii::$app->request instanceof \yii\web\Request ? [
  94. BaseActiveRecord::EVENT_BEFORE_INSERT => 'evaluateAttributes',
  95. BaseActiveRecord::EVENT_BEFORE_UPDATE => 'evaluateAttributes',
  96. BaseActiveRecord::EVENT_BEFORE_DELETE => 'evaluateAttributes',
  97. ] : [];
  98. }
  99. /**
  100. * Returns the column name to hold the version value as defined in [[\yii\db\BaseActiveRecord::optimisticLock()|optimisticLock()]].
  101. * @return string the property name.
  102. * @throws InvalidCallException if [[\yii\db\BaseActiveRecord::optimisticLock()|optimisticLock()]] is not properly configured.
  103. * @since 2.0.16
  104. */
  105. protected function getLockAttribute()
  106. {
  107. if ($this->_lockAttribute) {
  108. return $this->_lockAttribute;
  109. }
  110. /* @var $owner BaseActiveRecord */
  111. $owner = $this->owner;
  112. $lock = $owner->optimisticLock();
  113. if ($lock === null || $owner->hasAttribute($lock) === false) {
  114. throw new InvalidCallException("Unable to get the optimistic lock attribute. Probably 'optimisticLock()' method is misconfigured.");
  115. }
  116. $this->_lockAttribute = $lock;
  117. return $lock;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. *
  122. * In case of `null`, value will be parsed from [[\yii\web\Request::getBodyParam()|getBodyParam()]] or set to 0.
  123. */
  124. protected function getValue($event)
  125. {
  126. if ($this->value === null) {
  127. $request = Yii::$app->getRequest();
  128. $lock = $this->getLockAttribute();
  129. $formName = $this->owner->formName();
  130. $formValue = $formName ? ArrayHelper::getValue($request->getBodyParams(), $formName . '.' . $lock) : null;
  131. $input = $formValue ?: $request->getBodyParam($lock);
  132. $isValid = $input && (new NumberValidator())->validate($input);
  133. return $isValid ? $input : 0;
  134. }
  135. return parent::getValue($event);
  136. }
  137. /**
  138. * Upgrades the version value by one and stores it to database.
  139. *
  140. * ```php
  141. * $model->upgrade();
  142. * ```
  143. * @throws InvalidCallException if owner is a new record.
  144. * @since 2.0.16
  145. */
  146. public function upgrade()
  147. {
  148. /* @var $owner BaseActiveRecord */
  149. $owner = $this->owner;
  150. if ($owner->getIsNewRecord()) {
  151. throw new InvalidCallException('Upgrading the model version is not possible on a new record.');
  152. }
  153. $lock = $this->getLockAttribute();
  154. $version = $owner->$lock ?: 0;
  155. $owner->updateAttributes([$lock => $version + 1]);
  156. }
  157. }