本文實例講述了thinkPHP5.0框架事務處理操作。分享給大家供大家參考,具體如下:
事務的調用在mysql里需要注意下數(shù)據(jù)庫引擎,處理前先查看一下
刪除方法:
public function del() { $cate = new CateModel; $id=input('id'); $selectID=$cate->find($id); if($id == ''){ $this->error('請不要惡意測試'); } //調用事務刪除 $del=$cate->shiwu($id); if($del == true){ $this->success('刪除成功/!'); }else{ $this->error('刪除失敗/!'); } }
調用事務刪除
//事務處理刪除 public function shiwu($id) { $cates=Cate::getChildId($id); Db::startTrans($id,$cates); //$cates是所有子分類的一維數(shù)組 try{ Db::table('tp_cate')->where('id','in',$cates)->delete(); //刪除所有子分類 Db::table('tp_cate')->where('id',$id)->delete(); //刪除自身 // 提交事務 Db::commit(); return true; } catch (\Exception $e) { // 回滾事務 Db::rollback(); return false; } }
getChildId方法
public function getChildId($id) { $cateres=Cate::select(); return $this->_getChildId($cateres,$id); } public function _getChildId($cateres,$id) { static $arr = array(); foreach ($cateres as $k => $v) { if($id == $v['pid']){ $arr[] = $v['id']; $this->_getChildId($cateres,$v['id']); } } return $arr; }
更多關于thinkPHP相關內容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結》、《ThinkPHP常用方法總結》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術總結》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。