Laravel EloquentÄ£×ÓÖÐÀÖ¹ÛËøµÄʵÏÖ
±¾ÆªÎÄÕ¸ø¸÷ÈË´øÀ´Á˹ØÓÚlaravelµÄÏà¹Ø֪ʶ£¬ÆäÖÐÖ÷Òª¸ú¸÷ÈËÏÈÈÝlaravel eloquentÄ£×ÓÖÐÀÖ¹ÛËøµÄʵÏÖ£¬ÓдúÂëʾÀý£¬¸ÐÐËȤµÄÅóÙÏÂÃæÒ»ÆðÀ´¿´Ò»Ï°ɣ¬Ï£Íû¶Ô¸÷ÈËÓÐ×ÊÖú¡£
ÔÚapp/Utils/TraitsĿ¼Ï½¨ÉèOptimisticLockTrait.php£¬´úÂëÈçÏ£º
namespace App\Utils\Traits;use Illuminate\Database\Eloquent\Builder;trait OptimisticLockTrait{ /** * @var array $optimisticConditions * @var array $bindings */ protected $optimisticConditions, $bindings; /** * @var string $optimisticConditionRaw */ protected $optimisticConditionRaw; /** * save ʱÔöÌíÀÖ¹ÛËøÌõ¼þ * @param Builder $builder */ protected function performUpdate(Builder $builder) { if (!empty($this->optimisticConditions)) { foreach ($this->optimisticConditions as $field => $value) { if (is_array($value)) { $count = count($value); if ($count >= 3) { switch (strtoupper($value[1])) { case 'IN': $builder->whereIn($value[0], $value[2]); break; case 'NOT IN': $builder->whereNotIn($value[0], $value[2]); break; case 'BETWEEN': $builder->whereBetween($value[0], $value[2]); break; case 'NOT BETWEEN': $builder->whereNotBetween($value[0], $value[2]); break; default: $builder->where($value[0], $value[1], $value[2]); } } else { $builder->where($value); } } else { $builder->where($field, $value); } } } // ÔʼÌõ¼þ×¢Èë if ($this->optimisticConditionRaw) $builder->whereRaw($this->optimisticConditionRaw, $this->bindings); return $this->clearOptimistic()->perFormUpdating($builder); } /** * updating with optimistic * * @param Builder $builder * @return bool */ protected function perFormUpdating(Builder $builder) { // If the updating event returns false, we will cancel the update operation so // developers can hook Validation systems into their models and cancel this // operation if the model does not pass validation. Otherwise, we update. if ($this->fireModelEvent('updating') === false) { return false; } // First we need to create a fresh query instance and touch the creation and // update timestamp on the model which are maintained by us for developer // convenience. Then we will just continue saving the model instances. if ($this->usesTimestamps()) { $this->updateTimestamps(); } // Once we have run the update operation, we will fire the "updated" event for // this model instance. This will allow developers to hook into these after // models are updated, giving them a chance to do any special processing. $dirty = $this->getDirty(); $res = 0; if (count($dirty) > 0) { $res = $this->setKeysForSaveQuery($builder)->update($dirty); $this->syncChanges(); $this->fireModelEvent('updated', false); } return !empty($res); } // ɨ³ýÀÖ¹ÛËøÌõ¼þ function clearOptimistic() { $this->optimisticConditions = null; $this->optimisticConditionRaw = null; return $this; } // ÉèÖÃÀÖ¹ÛËøÌõ¼þ×Ö¶ÎÃûÁбí function setOptimistic(array $optimisticConditions) { $this->optimisticConditions = $optimisticConditions; return $this; } // ÉèÖÃÀÖ¹ÛËøÔʼÌõ¼þ×Ö¶ÎÃûÁбí function setOptimisticRaw(string $optimisticConditionRaw, array $bindings = []) { $this->optimisticConditionRaw = $optimisticConditionRaw; $this->bindings = $bindings; return $this; }}
µÇ¼ºó¸´ÖÆ
ÀÖ¹ÛËøʹÓÃ˵Ã÷
1¡¢ÔÚÄ£×ÓÖÐ(Models)»òÄ£×Ó¸¸ÀàʹÓÃ
/** * App\Models\BaseModel * @mixin \Eloquent * @method static \Illuminate\Database\Eloquent\Builder|BaseModel newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|BaseModel newQuery() * @method static \Illuminate\Database\Eloquent\Builder|BaseModel query() */class BaseModel extends Model{ use OptimisticLockTrait;}
µÇ¼ºó¸´ÖÆ
2¡¢Ê¹ÓÃÒªÁ죺
$ord = Order::find(1); $ord->payment_status = 1; if(!$model->setOptimistic(['payment_status' => 0]))->save()) throws new Exception('¶©µ¥ÒѸ¶¹ý¿îÁË');
µÇ¼ºó¸´ÖÆ
»òÕßʹÓÃÔʼSQL·½·¨:
$ord = Order::find(1); $ord->payment_status = 1; if(!$model->setOptimisticRaw('payment_status = ?',[1]))->save()) throws new Exception('¶©µ¥ÒѸ¶¹ý¿îÁË');
µÇ¼ºó¸´ÖÆ
ÈôÊÇͳһ¹¤¾ßСÉæ¼°µ½¶à´Î¸üУ¬Ôò¿ÉÒÔɨ³ýËøÌõ¼þ
$ord->clearOptimistic();
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇÀÖ¹ÛËøµÄʵÏÖ·½·¨£¬ÔÚÏÖʵ¿ª·¢ÖнÏÁ¿³£ÓÃÒ²ºÜÓÐÐëÒª¡£
ÍƼöѧϰ£º¡¶laravelÊÓƵ½Ì³Ì¡·
ÒÔÉϾÍÊÇLaravel EloquentÄ£×ÓÖÐÀÖ¹ÛËøµÄʵÏÖµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡
ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿Ê±ÊµÊ±ÐÞÕý»òɾ³ý¡£