在模型文件中写入以下代码:
/**
* @function 实现一个简单的laravel乐观锁
* @return int 受影响的函数
* @other void
*/
public function cas() {
//首先判断数据是否存在
if(! $this->exists) {
return 0;
}
//获取那些数据是被更改的
$changed_but_no_save = $this->getDirty();
if(! $changed_but_no_save) {
return 0;
}
//判断更改的字段和查询的字段是否一致
// array_keys($this->original) 相当于select的字段
if(array_diff(array_keys($changed_but_no_save), array_keys($this->original))) {
return 0;
}
//判断是否要更新时间戳
if($this->usesTimestamps()) {
$this->updateTimestamps();
$changed_but_no_save = $this->getDirty();
}
//获取更新时间字段的字段名
$update_field_name = $this->getUpdatedAtColumn();
$updated_at_val = $this->{$update_field_name};
$query = $this->query()->where($this->getKeyName(), $this->getKey())
->when($updated_at_val, function (Builder $query) use($update_field_name, $updated_at_val) {
$query->where($update_field_name, $updated_at_val);
});
foreach($changed_but_no_save as $k => $v) {
//getOriginal(field) 源数据
$query->where($k, $this->getOriginal($k));
}
$res = $query->update($changed_but_no_save);
//同步Eorm对象数据
if($res > 0) {
$this->syncChanges();
$this->syncOriginal();
}
return $res;
}
使用示例
$res = ModelName::find($id);
$res->field_name = value;
dd($res->cas());
验证(推荐使用日志的方式记录SQL日志)
//在app/Providers/AppServiceProvider.php 的boot方法中写入以下事件(laravel底层利用观察者模式记录SQL到日志)
DB::listen(function ($query) {
Log::channel('db')->info($query->sql, $query->bindings);
});
//附config/logging.php配置:
'db' => [
'driver' => 'daily',
'path' => storage_path('logs/db.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => 7,
],
/*
示例日志
[2022-10-04 01:07:48] local.INFO: select * from `test` where `test`.`id` = ? limit 1 [1]
[2022-10-04 01:07:48] local.INFO: update `test` set `s1` = ? where `id` = ? and `s1` = ? [6,1,"2"]
*/