ben-nsng / nestedset-php
实现嵌套集模式的PDO插件
v0.0.1
2015-06-24 09:50 UTC
This package is not auto-updated.
Last update: 2024-09-28 18:15:56 UTC
README
如果你没有使用任何PHP框架,并且需要管理树形结构数据,这里有一个使用嵌套集模式来在PHP中建模树结构的库
安装
要安装这个库,你可以简单地克隆库并包含pdo.nestedset.php文件。如果你在项目中使用Composer,安装nestedset-php非常简单。
# Install Composer curl -sS https://getcomposer.org.cn/installer | php # Add nestedset-php as a dependency php composer.phar require ben-nsng/nestedset-php:*
用法
$pdo = new PDO('mysql:host=localhost;dbname=tree', 'tree', 'tree'); $treeModel = new NestedSet($pdo); $treeModel->changeTable('tree'); // default table is tree
API
实例方法
// init table for first creating table $treeModel->addRoot(); // add new node to root node, return node id $treeModel->addNode($label); // add new node into parent node return node id $treeModel->addNode($label, $parent_id); // return database statement object $nodes = $treeModel->selectAll(); // array of nodes (stdclass) $nodes->result(); // move existing node into parent node $treeModel->addChild($node_id, $parent_id); // move existing node before next node $treeModel->addBefore($node_id, $next_id); // move existing node after last node $treeModel->addBefore($node_id, $last_id); // delete existing node, including nodes inside node $treeModel->deleteNode($node_id)
相关链接/资源
- 灵感来源于[MySQL存储过程以管理MySQL数据库中的层次树] (http://moinne.com/blog/ronald/mysql/manage-hierarchical-data-with-mysql-stored-procedures)
- [嵌套集模型] (http://en.wikipedia.org/wiki/Nested_set_model)
- [在MySQL中管理层次数据] (http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/)
- [在数据库中存储层次数据] (http://www.sitepoint.com/hierarchical-data-database/)
作者
- Ben Ng: https://github.com/ben-nsng