本文實例講述了thinkPHP5.0框架事務處理操作。分享給大家供大家參考,具體如下:
事務的調用在mysql里需要注意下數據庫引擎,處理前先查看一下
刪除方法:
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是所有子分類的一維數組 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框架的PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選