ThinkPHP6 模型事件用法

模型事件是指在进行模型的查询和写入操作的时候触发的操作行为

模型事件只在调用模型的方法生效,使用查询构造器操作是无效的

编号 事件 描述 事件方法名
1 after_read 查询后 onAfterRead
2 before_insert 新增前 onBeforeInsert
3 after_insert 新增后 onAfterInsert
4 before_update 更新前 onBeforeUpdate
5 after_update 更新后 onAfterUpdate
6 before_write 写入前 onBeforeWrite
7 after_write 写入后 onAfterWrite
8 before_delete 删除前 onBeforeDelete
9 after_delete 删除后 onAfterDelete
10 before_restore 恢复前 onBeforeRestore
11 after_restore 恢复后 onAfterRestore

namespace app\model;
use think\Model;
class Goods extends Model{
public function one_update(){
$update = Goods::update(
['price'=>'www.hezhidz.cn'],
['id'=>22]
);
return $update;
}
# 执行更新操作,就会之下onBeforeUpdate方法
public static function onBeforeUpdate($goods){
print_r($goods->price);
return true;
}
}

posted @ 2021-06-24 10:07  学无边涯  阅读(1444)  评论(0编辑  收藏  举报