thinkphp模型事件(鈎子函數:模型中在增刪改等操作前后自動執行的事件)
一、總結
1、通過模型事件(鈎子函數),可以在插入更新刪除等前后執行一些特定的功能
2、模型事件是寫在模型里面的,控制器中也必須用了模型操作數據庫才能觸發模型事件
3、模型事件是需要注冊的,注冊了才能使用,可用init統一注冊
4、支持傳入一個參數,(當前的模型對象實例),也就是你傳到模型中的數據,也就是你傳到數據庫中的數據
實例:圖片上傳
模型:
1 <?php 2 namespace app\admin\model; 3 use think\Model; 4 5 class Article extends Model 6 { 7 protected static function init() //用來統一注冊,里面的事件注冊了才能用 8 { 9 Article::event('before_insert', function ($datain) { //支持傳入一個參數,(當前的模型對象實例),也就是你傳到模型中的數據 10 if($_FILES['athumb']['tmp_name']){ 11 // 獲取表單上傳文件 例如上傳了001.jpg 12 $file = request()->file('athumb'); 13 // 移動到框架應用根目錄/public/uploads/ 目錄下 14 $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads/admin'); 15 // 已經上傳成功,我們要把文件的路徑寫進數據庫 16 $datain['athumb']='/static/uploads/admin/'.$info->getSaveName(); 17 } 18 }); 19 } 20 }
控制器:
<?php namespace app\admin\controller; use think\Controller; use app\admin\model\Article as ModelArticle; use app\admin\model\Cate as ModelCate; use app\admin\controller\Base; class Article extends Base { public function add(){ if(request()->isPost()){ $datain=input('post.'); $datain['time']=time(); $modelArticle=new ModelArticle(); //2、獲取傳入的文件數據 /*在模型的事件中處理了*/ if($modelArticle->save($datain)){ //用了模型,傳入的參數就是$datain $this->success('添加文章成功!!','article/lst'); }else{ $this->error('添加文章失敗!!'); } // dump($datain);die; return; } //將欄目信息顯示回增加界面 //catetree方法新增了欄目的level字段 $modelCate=new ModelCate(); $data=$modelCate->catetree(); $this->assign('data',$data); return view(); } }
二、thinkphp模型事件(鈎子函數)
模型事件
版本 | 新增功能 |
---|---|
5.0.4 | 增加模型事件注冊快捷方法 |
模型事件是指在進行模型的寫入操作的時候觸發的操作行為,包括模型的save方法和delete方法。
模型事件只可以在調用模型的方法才能生效,使用查詢構造器通過Db類操作是無效的
模型類支持before_delete
、after_delete
、before_write
、after_write
、before_update
、after_update
、before_insert
、after_insert
事件行為
標簽位 | 描述 |
---|---|
before_insert | 新增前 |
after_insert | 新增后 |
before_update | 更新前 |
after_update | 更新后 |
before_write | 寫入前 |
after_write | 寫入后 |
before_delete | 刪除前 |
after_delete | 刪除后 |
使用方法如下:
User::event('before_insert', function ($user) { if ($user->status != 1) { return false; } });
注冊的回調方法支持傳入一個參數(當前的模型對象實例),並且before_write
、before_insert
、 before_update
、before_delete
事件方法如果返回false,則不會繼續執行。
支持給一個位置注冊多個回調方法,例如:
User::event('before_insert', function ($user) { if ($user->status != 1) { return false; } }); // 注冊回調到beforeInsert函數 User::event('before_insert', 'beforeInsert');
可以在模型類的init方法里面統一注冊模型事件,例如:
namespace app\index\model; use think\Model; class User extends Model { protected static function init() { User::event('before_insert', function ($user) { if ($user->status != 1) { return false; } }); } }
調用當前模型也可以寫入 self::event('before_insert', ...)
快捷注冊(V5.0.4+
)
V5.0.4+版本開始,系統提供了內置的事件注冊的快捷方法,你可以用下面的方式替代
namespace app\index\model; use think\Model; class User extends Model { protected static function init() { User::beforeInsert(function ($user) { if ($user->status != 1) { return false; } }); } }
這些模型類的快捷方法如下:
標簽位 | 描述 |
---|---|
beforeInsert | 新增前 |
afterInsert | 新增后 |
beforeUpdate | 更新前 |
afterUpdate | 更新后 |
beforeWrite | 寫入前 |
afterWrite | 寫入后 |
beforeDelete | 刪除前 |
afterDelete | 刪除后 |