分类树操作类

类概述

phpGrace 封装了基于二叉树结构的树状分类管理工具类 ( tree ) ,利用它可以开发出更高效的无限分类系统;
二叉树优点 : 查询速度极快的无限级分类解决方案;
二叉树缺点 : 分类改动需要全局左右值更新,更新、添加效率不高;

数据表结构

请根据业务需要制定数据表名称并传递给分类工具类;

CREATE TABLE `grace_categories` (
  `cate_id` int(10) NOT NULL AUTO_INCREMENT,
  `cate_name` varchar(200) DEFAULT NULL COMMENT '分类名称',
  `cate_desc` text COMMENT '分类描述',
  `cate_left` int(10) DEFAULT NULL COMMENT '左值',
  `cate_right` int(10) DEFAULT NULL COMMENT '右值',
  `cate_order` int(10) DEFAULT NULL COMMENT '类分排序',
  PRIMARY KEY (`cate_id`),
  UNIQUE KEY `cate_order` (`cate_order`) USING BTREE,
  KEY `cate_left` (`cate_left`),
  KEY `cate_right` (`cate_right`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

-- ----------------------------
-- 需要一条基础数据作为树的起点
-- ----------------------------
INSERT INTO `grace_categories` VALUES ('1', 'witCte', '请勿删除', '0', '1', '0');

相关函数

addNode() 添加节点

/**
 * 功能 : 增加节点
 * @param  int    $parentId     父级分类 ID
 * @param  string $name         分类名称
 * @param  string $desc         分类描述
 * @param  int    $order        分类排序
 */

// 示例代码
class indexController extends grace{
    public function index(){
        $tree = new phpGrace\tools\tree('categories');
        $tree->addNode(1, '一级分类 01','一级分类 01 描述', 10000);
    }
}

delete() 删除节点

/**
 * 功能 : 删除节点及其自节点
 * @param  int    $id     节点id
 */

// 示例代码
class indexController extends grace{
    public function index(){
        $tree = new phpGrace\tools\tree('categories');
        $tree->delete(38);
    }
}

showTableTree() 输出表格形式的树状数据

/**
 * 功能 : 输出表格形式的树状数据
 * @param  int    $parentId     父级节点 id
 */

// 演示代码
class indexController extends grace{
    public function index(){
        $tree = new phpGrace\tools\tree('categories');
        $tree->showTableTree(1);
    }
}

showOptionTree()输出 option 形式的树状数据

/**
 * 功能 : 输出 option 形式的树状数据
 * @param  int    $parentId     父级节点 id
 */

// 演示代码
class indexController extends grace{
    public function index(){
        $tree = new phpGrace\tools\showOptionTree('categories');
        $tree->showTableTree(1);
    }
}

getSons()获取符合条件的所有节点

/**
 * 功能 : 获取符合条件的所有节点
 * @param  string  $CatagoryID 节点id
 * @param  int $type 1,所有子类,不包含自己; 2包含自己的所有子类; 3不包含自己所有父类 4;包含自己所有父类
 */

// 演示代码
class indexController extends grace{
    public function index(){
        $tree = new phpGrace\tools\tree('categories');
        $sons = $tree->getSons(1);
        p($sons);
    }
}

getNodeById()移动节点

/**
 * 功能 : 移动节点
 * @param  int  $SelfCatagoryID 需要移动的节点 id
 * @param  int  $ParentCatagoryID 移动到具体父级节点的 id
 */

// 演示代码
class indexController extends grace{
    public function index(){
        $tree = new phpGrace\tools\tree('categories');
        $node = $tree->getNodeById(37);
        p($node);
    }
}

getNodeByName()根据节点名称查询节点信息

/**
 * 功能 : 根据节点名称查询节点信息
 * @param  string  $name 节点名称
 */

// 演示代码
class indexController extends grace{
    public function index(){
        $tree = new phpGrace\tools\tree('categories');
        $node = $tree->getNodeByName('2级分类 0102');
        p($node);
    }
}

getNodes()获取符合条件的所有节点

/**
 * 功能 : 获取符合条件的所有节点
 * @param  string  $CatagoryID 节点id
 * @param  int $type 1 所有子类,不包含自己; 2 包含自己的所有子类; 3 不包含自己所有父类; 4 包含自己所有父类
 */

// 演示代码
class indexController extends grace{
    public function index(){
        $tree  = new phpGrace\tools\tree('categories');
        $nodes = $tree->getNodes(1);
        p($nodes);
    }
}

moveNode()移动节点

/**
 * 功能 : 移动节点
 * @param  int  $SelfCatagoryID 需要移动的节点 id
 * @param  int  $ParentCatagoryID 移动到具体父级节点的 id
 */

// 演示代码
class indexController extends grace{
    public function index(){
        $tree  = new phpGrace\tools\tree('categories');
        $tree->moveNode(57, 56);
    }
}

getParentNode()获取父节点

/**
 * 功能 : 获取父节点
 * @param  int  $nodeId 需要移动的节点 id
 * @param  int  $type   查询类型, 1 : 直属父类, 0 : 所有父类
 */
 
// 演示代码
class indexController extends grace{
    public function index(){
        $tree  = new phpGrace\tools\tree('categories');
        $parentNodes = $tree->getParentNode(57, 1);
        p($parentNodes);
    }
}