thinkphp 的左右值無限分類

<?php
class CommonCategoryModel extends Model {
        /**
     * 獲取孩子節點
     * all==false時,只獲取直接子節點,
     * self==false時,不包括本身
     *
     *
     * @param unknown_type $id
     * @param unknown_type $all
     */
    public function getChildren($id,$self=true,$all=true) {
         if(intval($id)){
             $category =$this->find($id);
             $left = $category['left'];
             $right = $category['right'];
             $level = $category['level'];
             $where = "`left` BETWEEN $left AND $right";
             if(!$all){
                 //不取所有,只取直接孩子
                 $where .=" and level = $level+1";
             }
             if(!$self){
                 //不包括本身
                 $where .=" and level  > $level";
             }
             return $this->where($where)->order('`left` asc')->select();
         }
    }
    /**
     * 獲取父類節點,若是all==false,則只取直接父類節點
     * self==false,不包括本身
     *
     * @param unknown_type $id
     * @param unknown_type $self
     * @param unknown_type $all
     */
    public function getParents($id,$self=false,$all=true,$root = false){
        if(intval($id)){
            $category =$this->find($id);
             $left = $category['left'];
             $right = $category['right'];
             $level = $category['level'];
             if($level>0){
             if(!$self){
             $where = "`left` < $left AND `right` > $right";
             }else{
                 $where = "`left` <= $left AND `right` >= $right";
             }
             if(!$root){
                 $where .= " and level!=0";
             }
             if(!$all){
                 $where .=" and level  = $level-1";
             }
             return $this->where($where)->order('`left` asc')->select();
             }else{
                 if($self){
                     return $category;
                 }else{
                     return null;
                 }
             }
        }
    }
    /**
     * 增長子節點
     *
     * @param unknown_type $id
     * @param unknown_type $childName
     * @return unknown
     */
    public function addChild($id,$childName){
        if(intval($id)){
            $category =$this->find($id);
             $left = $category['left'];
             $right = $category['right'];
             $level = $category['level'];
            $this->query("UPDATE __TABLE__ SET `left`=`left`+2 WHERE `left` >$right");
            $this->query("UPDATE __TABLE__ SET `right`=`right`+2 WHERE `right`>=$right");
            $data['left'] = $right;
            $data['right'] = $right+1;
            $data['level'] = $level+1;
            $data['name'] = $childName;
            return $this->add($data);
        }
    }
    /**
     * 刪除節點
     *
     * @param unknown_type $id
     */
    public function deleteNode($id){
        if($id === 1 ){
            //若是是根目錄,設定爲不可刪除
            return false;
            //$thi->query("truncate table __TABLE__ ");
        }else{
            $category =$this->find($id);
            $left = $category['left'];
            $right = $category['right'];
            $level = $category['level'];
            if($level ==0){//仍然是根節點
            //    $this->query("truncate table __TABLE__ ");
                return false;
            }
            //非根節點,且存在,刪除本身以及其子孫
            $this->where("`left`>={$left} and `right`<=$right")->delete();
            //更新左右值
            $value=$right-$left+1;
            $this->query("UPDATE __TABLE__ SET `left` =`left` - $value WHERE `left`> $left");
            $this->query("UPDATE __TABLE__ SET `right`=`right`- $value WHERE `right`>$right");

        }
    }
    
    
    public  function getModelName() {
       return substr(get_class($this),0,-5);
    }
}
?>php

 

使用:this

其餘的model extends commonCategoryModelget

相關文章
相關標籤/搜索