功能在模型内部更新时没有被触发
P粉752826008
P粉752826008 2023-08-08 16:31:55
[PHP讨论组]
<p>我有一个像这样的模型:</p> <pre class="brush:php;toolbar:false;">class Equipment extends Model { use HasFactory,softDeletes; protected $table = 'equipments'; protected $fillable = ['station_id', 'parent_id', 'code', 'name', 'description', 'creator_id','deletor_id','updator_id']; protected $softDelete = true; protected $dates = ['deleted_at']; public static function boot() { parent::boot(); //it doesn't called at all! static::updated(function (Model $model) { Log::error('calling refreshTree'); $model-&gt;refreshTree(); }); static::created(function (Model $model) { $model-&gt;refreshTree(); }); static::deleted(function (Model $model) { $model-&gt;refreshTree(); }); } public function refreshTree(){ Log::error('refreshTree'); $equipment = DB::table('equipments')-&gt;get(); $treeData = $this-&gt;generateTree($equipment); $jsonData = json_encode($treeData); Redis::set(config('redis.EquipmentTree'),$jsonData); } private function generateTree($data, $parentId = 0) { $tree = []; foreach ($data as $item) { if ($item-&gt;parent_id == $parentId) { $children = $this-&gt;generateTree($data, $item-&gt;id); $node = [ 'id' =&gt; $item-&gt;id, 'text' =&gt; $item-&gt;name, 'editURL'=&gt;route('dashboard.basic-info.equipments.edit',['id'=&gt;$item-&gt;id]), 'children' =&gt; $children ]; if(count($children) &lt;= 0) unset($node['children']); array_push($tree, $node); } } return $tree; } }</pre> <p>当我创建模型时,创建函数被触发,但当我更新模型时,更新函数没有被触发</p> <pre class="brush:php;toolbar:false;">//Equipment::where('id',$id)-&gt;update(['parent_id'=&gt;$recordTarget['id']]); //它没有生效 //我也尝试了这个: $equipment = Equipment::find($id); $equipment-&gt;parent_id = $recordTarget['id']; $equipment-&gt;save();</pre> <p><br /></p>
P粉752826008
P粉752826008

全部回复(1)
P粉165522886

当使用查询构建器实例或批量更新时,即使你只针对一行进行操作,事件监听器也不会触发

Equipment::where('id',$id) //right here you get a Builder instance
    ->update([]); //will not trigger the event listener as it is a mass update

要触发它,你需要使用模型实例

Equipment::where('id',$id)->first() //This returns a model instance
    ->update([]); //will trigger the event listener as it is a single instance update

这也等同于

$equipment = Equipment::find($id);
$equipment->parent_id = ..;
$equipment->save();

并且你可以看到,在模型上调用update()与在查询构建器上调用update()是不同的。

当你考虑到,要触发这些事件,代码需要一个模型的实例来与之一起工作,就像static::updated(function (Model $model) {那样。如果你的查询不同,例如Equipment::where('id','>',$id),为了处理所有事件,它需要查询所有受影响的行,为它们生成模型实例,然后使用这些模型调用事件。

这会降低性能

但是如果没有其他办法,你可以明确地这样做

$equipments = Equipment::where('id','>',$id)->get();
foreach ($equipments as $equipment) {
    $equipment->update(['parent_id'=>$recordTarget['id']]);
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号