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;
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM