Home> PHP Framework> Laravel> body text

Implementation of optimistic locking in Laravel Eloquent model

藏色散人
Release: 2023-04-21 15:53:05
forward
960 people have browsed it

This article brings you relevant knowledge about Laravel. It mainly introduces to you the implementation of optimistic locking in the Laravel Eloquent model. There are code examples. Friends who are interested can take a look below. I hope it will be useful to you. help.

Create OptimisticLockTrait.php in the app/Utils/Traits directory, the code is as follows:

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; }}
Copy after login

Instructions for using optimistic lock

1. Use

/** * 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;}
Copy after login

in the model (Models) or model parent class. 2. Usage method:

$ord = Order::find(1); $ord->payment_status = 1; if(!$model->setOptimistic(['payment_status' => 0]))->save()) throws new Exception('订单已付过款了');
Copy after login

or use the original SQL method:

$ord = Order::find(1); $ord->payment_status = 1; if(!$model->setOptimisticRaw('payment_status = ?',[1]))->save()) throws new Exception('订单已付过款了');
Copy after login

If the same object is small If multiple updates are involved, the lock condition can be cleared

$ord->clearOptimistic();
Copy after login

The above is the implementation method of optimistic locking, which is commonly used and necessary in actual development.

Recommended learning: "laravel video tutorial"

The above is the detailed content of Implementation of optimistic locking in Laravel Eloquent model. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!