tp无限极分类批量删除 单个删除

总体思路,无限极删除要取到每个子类的主id

可以使用数据库循环查询,但是太浪费资源。所以使用tp的_before_delete()函数

直接上代码

//根据id查找子类的id
public function getcharl($cate_id){
            static $re=array();
            $data=$this->select();
            foreach ($data as $k=>$v){
             if($v['parentid']==$cate_id){
                  $re[]=$v["cate_id"];
                  $this->getcharl($data,$v['cate_id']);
             }
      }
      return $re;
}
public function _before_delete($option){
         //如果查询记过是函数就是批量删除 否则是单个删除
        if(is_array($option['where']['cate_id'])){
             $ar=explode(',',$option['where']['cate_id'][1]);
            $soncates=array();
             foreach ($ar as $k=>$v){
                  $conten2=$this->getcharl($v);
                 $soncates =array_merge($soncates,$conten2);
             }
            $soncates= array_unique( $soncates);
            $soncates=implode(",", $soncates);
            if($soncates){
                $this->execute("delete from ar_category where cate_id in ($soncates)");
            }
        }else{
            $chilrenids=$this->getcharl($option['where']['cate_id']);//根据id查找子类的cate_id
            $chilrenids=implode(',',$chilrenids);//拆分成字符串
            if($chilrenids){
                $this->execute("delete from ar_category where cate_id in ($chilrenids)");
            }
        }

}

猜你喜欢

转载自blog.csdn.net/xueshao110/article/details/80371155