rundiz/nested-set
Composer 安装命令:
composer require rundiz/nested-set
包简介
class library for CRUD the nested set data such as category tree.
关键字:
README 文档
README
The PHP nested set model for create/read/update/delete the tree data structure (hierarchy).
A class that help you to query complex data of nested set.
Tested up to PHP 8.5.
Example
Install
I recommend you to install this library via Composer and use Composer autoload for easily include the file. If you are not using Composer, you have to manually include the file by yourself.
Please make sure that the path to files are correct.
include_once '/path/to/Rundiz/src/NestedSet.php';
If you want to make sure or see how class is working please following the step below, otherwise skip this step.
Import tests/common/test-database-structure.sql file to the database.
Connect to DB
This class is using PDO class. Please connect to PDO and send the \PDO object to the class constructor.
$db['dsn'] = 'mysql:dbname=YOUR_DB_NAME;host=localhost;port=3306;charset=UTF8'; $db['username'] = 'admin'; $db['password'] = 'pass'; $db['options'] = [ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION // throws PDOException. ]; $PDO = new \PDO($dbConfig['dsn'], $dbConfig['username'], $dbConfig['password'], $dbConfig['options']); $NestedSet = new \Rundiz\NestedSet\NestedSet($PDO); $NestedSet->tableName = 'test_taxonomy';// this should be your table name but you can use this for testing.
Insert the data
Before insert, you can get new position of that taxonomy level using getNewPosition() method.
$new_position = $NestedSet->getNewPosition(4);// result is 4.
Everytime you insert the data, you have to run rebuild() method to generate level, left, right data. The incorrect level, left, right data can cause incorrect listing.
$stmt = $PDO->prepare('INSERT INTO `test_taxonomy`(`parent_id`, `name`, `position`) VALUES (?, ?, ?)'); $stmt->execute([0, 'Root 4', 4]); $NestedSet->rebuild();
Update the data
To update the data, you don't need new position for this but you have to run rebuild() method every time.
$stmt = $PDO->prepare('UPDATE `test_taxonomy`SET `name` = ?, `position` = ? WHERE `id` = ?; $stmt->execute(['Root 4 new name', 4, 21]); $NestedSet->rebuild();
Check parent under children
If you want to change the parent of selected item, you can check first that the new parent of selected item is under children of selected item or not.
You can use isParentUnderMyChildren() method to check this and false means correct parent (new parent is not children of editing item).
To continue on this please use the data in demo-data.sql file.
$editing_item_id = 9; $new_parent_id = 7; var_dump($NestedSet->isParentUnderMyChildren($editing_item_id, $new_parent_id));// false (CORRECT! the new parent is not child of this item) $new_parent_id = 14; var_dump($NestedSet->isParentUnderMyChildren($editing_item_id, $new_parent_id));// true (INCORRECT! the new parent is child of this item)
Read the data
To read the selected item data with its children, you can use getTaxonomyWithChildren() method.
$options['filter_taxonomy_id'] = 3;// The selected item ID. $list_txn = $NestedSet->getTaxonomyWithChildren($options); unset($options); print_r($list_txn);
To read the selected item data with its parent in a line until root item, you can use getTaxonomyWithParents() method.
$options['filter_taxonomy_id'] = 13;// The selected item ID. $list_txn = $NestedSet->getTaxonomyWithParents($options); unset($options); print_r($list_txn);
List the items
You can list the items by use listTaxonomy() method for nested array data or use listTaxonomyFlatten() for flatten data.
$options = []; $options['unlimited'] = true; $list_txn = $NestedSet->listTaxonomy($options); unset($options); // The variable $list_txn is array and have 2 keys (total, items).
Both methods parameters are same.
Delete an item
You can choose how to delete an item.
- Delete selected item and ALL of its children.
- Delete selected item and pull up its children to the current parent.
Every time you deleted, you have to run the rebuild() method to correct level, left, right data.
Delete selected item and ALL of its children.
$NestedSet->deleteWithChildren(16); $NestedSet->rebuild();
Delete selected item and pull up its children to the current parent.
$NestedSet->deletePullUpChildren(9); $NestedSet->rebuild();
For more example including complex conditions which is new feature since v 1.x, please take a look in tests/phpunit folder or the API document in .wiki/apidoc folder.
rundiz/nested-set 适用场景与选型建议
rundiz/nested-set 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.12k 次下载、GitHub Stars 达 34, 最近一次更新时间为 2016 年 08 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「category」 「categories」 「nested set」 「hierarchy」 「nested tree」 「sub category」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rundiz/nested-set 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rundiz/nested-set 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rundiz/nested-set 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Categories for NetCommons Plugin
PHP Interface for Babel Street Text Analytics
Adjacency List’ed Closure Table database design pattern implementation for Laravel. Includes restore of tree
Baum is an implementation of the Nested Set pattern for Eloquent models.
Nested Set Model for Laravel
Nested field validation for Yii2
统计信息
- 总下载量: 3.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 35
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-27
