Thinkphp5中欄目刪除功能

控制器層:

public functiondel(){

        $cateid = input('id');

        //$res =Db::table('tp_cate')->delete($cateid); //數據庫方法

        $res = db('cate')->delete($cateid);   //助手函數

        if($res){

            $this->success('刪除欄目成功!',url('lst'));

        }else{

            $this->error('刪除欄目失敗');

        }

    }

 

   //前置操作方法(刪除欄目前先刪除子欄目)

    protected $beforeActionList = [

        'delchilresid'  => ['only'=>'del'],

    ];

 

//前置操作方法中可以直接接收傳過來的參數

    public function delchilresid(){

        $cateid = input('id');

        $cate = new CateModel();

        $sonids =$cate->getchilresid($cateid);

        if($sonids){

           //Db::table('tp_cate')->delete($sonids);   //數據庫方法

           db('cate')->delete($sonids);  //助手函數

        }

}

 

模型層:

public function getchilresid($cateid){

        $cateres =$this->select();

        return$this->_getchilresid($cateres,$cateid);

    }

 

//用遞歸函數來查詢子欄目

    public function_getchilresid($cateres,$cateid){

        static $arr = array();

        foreach ($cateres as$k => $v) {

           if($v['pid']==$cateid){

                $arr[] =$v['id'];

               $this->_getchilresid($cateres,$v['id']);

            }

        }

        return $arr;

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章