Die Funktion wird nicht ausgelöst, wenn sie innerhalb des Modells aktualisiert wird
P粉752826008
2023-08-08 16:31:55
<p>我有一个像这样的模型:</p>
<pre class="brush:php;toolbar:false;">class Equipment erweitert Modell
{
verwenden Sie HasFactory,softDeletes;
protected $table = 'Ausrüstung';
protected $fillable = ['station_id', 'parent_id', 'code', 'name', 'description', 'creator_id','deletor_id','updator_id'];
protected $softDelete = true;
protected $dates = ['deleted_at'];
öffentliche statische Funktion boot()
{
parent::boot();
//Es ruft überhaupt nicht an!
static::updated(function (Model $model) {
Log::error('calling restartTree');
$model->refreshTree();
});
static::created(function (Model $model) {
$model->refreshTree();
});
static::deleted(function (Model $model) {
$model->refreshTree();
});
}
öffentliche Funktion restartTree(){
Log::error('refreshTree');
$equipment = DB::table('equipments')->get();
$treeData = $this->generateTree($equipment);
$jsonData = json_encode($treeData);
Redis::set(config('redis.EquipmentTree'),$jsonData);
}
private Funktion genericTree($data, $parentId = 0) {
$tree = [];
foreach ($data as $item) {
if ($item->parent_id == $parentId) {
$children = $this->generateTree($data, $item->id);
$node = [
'id' => $item->id,
'text' => $item->name,
'editURL'=>route('dashboard.basic-info.equipments.edit',['id'=>$item->id]),
'Kinder' => $Kinder
];
if(count($children) <= 0)
unset($node['children']);
array_push($tree, $node);
}
}
return $tree;
}
}</pre>
<p>
<pre class="brush:php;toolbar:false;">//Equipment::where('id',$id)->update(['parent_id'=>$recordTarget['id'] ]); //它没有生效
//我也尝试了这个:
$equipment = Ausrüstung::find($id);
$equipment->parent_id = $recordTarget['id'];
$equipment->save();</pre>
<p><br /></p>
当使用查询构建器实例或批量更新时,即使你只针对一行进行操作,事件监听器也不会触发
要触发它,你需要使用模型实例
这也等同于
并且你可以看到,在模型上调用update()与在查询构建器上调用update()是不同的。
当你考虑到,要触发这些事件,代码需要一个模型的实例来与之一起工作,就像static::updated(function (Model $model) {那样。如果你的查询不同,例如Equipment::where('id','>',$id),为了处理所有事件,它需要查询所有受影响的行,为它们生成模型实例,然后使用这些模型调用事件。
这会降低性能
但是如果没有其他办法,你可以明确地这样做