Tp5 模型事件的使用


Tp5 模型事件是指在進行模型的寫入操作的時候觸發的操作行為,包括模型的save方法和delete方法。

模型事件只可以在調用模型的方法才能生效,使用查詢構造器通過Db類操作是無效的

模型類支持before_delete、after_delete、before_write、after_write、before_update、after_update、before_insert、after_insert事件行為。

快捷注冊(V5.0.4+)

V5.0.4+版本開始,系統提供了內置的事件注冊的快捷方法,你可以用下面的方式替代

beforeInsert新增前

afterInsert新增后

beforeUpdate更新前

afterUpdate更新后

beforeWrite寫入前

afterWrite寫入后

beforeDelete刪除前

afterDelete刪除后

官方demo:

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

以下是處理商品模板使用方法說明:

model 模型代碼:

<?php namespace app\admin\model; use think\Model; class Goods extends Model { protected $field=true; protected static function init() { Goods::beforeInsert(function ($goods) { // 新增前,生成商品主圖的縮略圖 if($_FILES['og_thumb']['tmp_name']){ $thumbName=$goods->upload('og_thumb'); } }); Goods::afterInsert(function($goods){ // 新增后,批量寫入會員價格 $mpriceArr=$goods->mp; $goodsId=$goods->id; db('member_price')->insert(['mlevel_id'=>$k,'mprice'=>$v,'goods_id'=>$goodsId]); // 處理商品相冊 if($goods->_hasImgs($_FILES['goods_photo']['tmp_name'])){ $files = request()->file('goods_photo'); foreach($files as $file){ // 移動到框架應用根目錄/public/uploads/ 目錄下 $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads/goods'); if($info){ $photoName= $info->getFilename(); $ogphoto=date('Ymd').DS.$photoName; $bigphoto=date('Ymd').DS.'big_'.$photoName; $midphoto=date('Ymd').DS.'mid_'.$photoName; $smphoto=date('Ymd').DS.'sm_'.$photoName; $image = \think\Image::open(IMG_UPLAODS.'goods/'.$ogphoto); $image->thumb(500, 500)->save(IMG_UPLAODS.'goods/'.$bigphoto); $image->thumb(200, 200)->save(IMG_UPLAODS.'goods/'.$midphoto); $image->thumb(70, 70)->save(IMG_UPLAODS.'goods/'.$smphoto); db('goods_photo')->insert(['goods_id'=>$goodsId,'big_photo'=>'goods/'.$bigphoto,'mid_photo'=>'goods/'.$midphoto,'sm_photo'=>'goods/'.$smphoto,'og_photo'=>'goods/'.$ogphoto]); }else{ // 上傳失敗獲取錯誤信息 echo $file->getError(); } } } }); Goods::beforeDelete(function ($goods) { $goodsId=$goods->id; // 刪除前,刪除關聯的商品屬性 db('goods_attr')->where('goods_id','=',$goodsId)->delete(); }); } public function upload($imageName){ // 獲取表單上傳文件 例如上傳了001.jpg $file = request()->file($imageName); // 移動到框架應用根目錄/public/uploads/ 目錄下 if($file){ $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads/goods'); if($info){ // // 成功上傳后 獲取上傳信息 // // 輸出 jpg // echo $info->getExtension(); // // 輸出 20160820/42a79759f284b767dfcb2a0197904287.jpg // echo $info->getSaveName(); // // 輸出 42a79759f284b767dfcb2a0197904287.jpg return $info->getFilename(); }else{ // 上傳失敗獲取錯誤信息 echo $file->getError(); } } } }

controller 控制器代碼:

<?php namespace app\admin\controller; use think\Controller; class Goods extends controller {     public function add()     {      if(request()->isPost()){      $data=input('post.');      $add=model('goods')->save($data);      if($add){      $this->success('操作成功!','lst');      }else{      $this->error('操作失敗!');      }      return;      }         return view();     }     public function del($id)     {        $del=model('goods')->destroy($id);        if($del){ $this->success('操作成功!','lst'); }else{ $this->error('操作失敗!'); }     } }


免責聲明!

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



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