wbraganca/yii2-nested-set-behavior
Composer 安装命令:
composer require wbraganca/yii2-nested-set-behavior
包简介
This extension allows you to get functional for nested set trees.
README 文档
README
This extension allows you to get functional for nested set trees.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require wbraganca/yii2-nested-set-behavior "*"
or add
"wbraganca/yii2-nested-set-behavior": "*"
to the require section of your composer.json file.
Configuring
First you need to configure model as follows:
use wbraganca\behaviors\NestedSetBehavior; use wbraganca\behaviors\NestedSetQuery; class Category extends ActiveRecord { public function behaviors() { return [ [ 'class' => NestedSetBehavior::className(), // 'rootAttribute' => 'root', // 'levelAttribute' => 'level', // 'hasManyRoots' => true ], ]; } public static function find() { return new NestedSetQuery(get_called_class()); } }
There is no need to validate fields specified in leftAttribute,
rightAttribute, rootAttribute and levelAttribute options. Moreover,
there could be problems if there are validation rules for these. Please
check if there are no rules for fields mentioned in model's rules() method.
In case of storing a single tree per database, DB structure can be built with
schema/schema.sql. If you're going to store multiple trees you'll need
schema/schema-many-roots.sql.
By default leftAttribute, rightAttribute and levelAttribute values are
matching field names in default DB schemas so you can skip configuring these.
There are two ways this behavior can work: one tree per table and multiple trees
per table. The mode is selected based on the value of hasManyRoots option that
is false by default meaning single tree mode. In multiple trees mode you can
set rootAttribute option to match existing field in the table storing the tree.
Selecting from a tree
In the following we'll use an example model Category with the following in its
DB:
- 1. Mobile phones
- 2. iPhone
- 3. Samsung
- 4. X100
- 5. C200
- 6. Motorola
- 7. Cars
- 8. Audi
- 9. Ford
- 10. Mercedes
In this example we have two trees. Tree roots are ones with ID=1 and ID=7.
Getting all roots
$roots = Category::find()->roots()->all();
Result:
Array of Active Record objects corresponding to Mobile phones and Cars nodes.
Getting all descendants of a node
$category = Category::findOne(1); if ($category) { $descendants = $category->descendants()->all(); var_dump($descendants); }
Result:
Array of Active Record objects corresponding to iPhone, Samsung, X100, C200 and Motorola.
Getting all children of a node
$category = Category::findOne(1); if ($category) { $descendants = $category->children()->all(); var_dump($descendants); }
Result:
Array of Active Record objects corresponding to iPhone, Samsung and Motorola.
Getting all ancestors of a node
$category = Category::findOne(5); if ($category) { $ancestors = $category->ancestors()->all(); var_dump($ancestors); }
Result:
Array of Active Record objects corresponding to Samsung and Mobile phones.
Getting parent of a node
$category = Category::findOne(9); if ($category) { $parent = $category->parent()->one(); var_dump($parent); }
Result:
Array of Active Record objects corresponding to Cars.
Getting node siblings
Using NestedSet::prev() or
NestedSet::next():
$category = Category::findOne(9); if ($category) $nextSibling = $category->next()->one(); }
Result:
Array of Active Record objects corresponding to Mercedes.
Getting the whole tree
You can get the whole tree using standard AR methods like the following.
For single tree per table:
Category::find()->addOrderBy('lft')->all();
For multiple trees per table:
Category::find()->andWhere('root = ?', [$root_id])->addOrderBy('lft')->all();
Modifying a tree
In this section we'll build a tree like the one used in the previous section.
Creating root nodes
You can create a root node using NestedSet::saveNode().
$root = new Category; $root->title = 'Mobile Phones'; $root->saveNode(); $root = new Category; $root->title = 'Cars'; $root->saveNode();
Result:
- 1. Mobile Phones
- 2. Cars
Adding child nodes
There are multiple methods allowing you adding child nodes. To get more info about these refer to API. Let's use these to add nodes to the tree we have:
$category1 = new Category; $category1->title = 'Ford'; $category2 = new Category; $category2->title = 'Mercedes'; $category3 = new Category; $category3->title = 'Audi'; $root = Category::findOne(1); $category1->appendTo($root); $category2->insertAfter($category1); $category3->insertBefore($category1);
Result:
- 1. Mobile phones
- 3. Audi
- 4. Ford
- 5. Mercedes
- 2. Cars
Logically the tree above doesn't looks correct. We'll fix it later.
$category1 = new Category; $category1->title = 'Samsung'; $category2 = new Category; $category2->title = 'Motorola'; $category3 = new Category; $category3->title = 'iPhone'; $root = Category::findOne(2); $category1->appendTo($root); $category2->insertAfter($category1); $category3->prependTo($root);
Result:
- 1. Mobile phones
- 3. Audi
- 4. Ford
- 5. Mercedes
- 2. Cars
- 6. iPhone
- 7. Samsung
- 8. Motorola
$category1 = new Category; $category1->title = 'X100'; $category2 = new Category; $category2->title = 'C200'; $node = Category::findOne(3); $category1->appendTo($node); $category2->prependTo($node);
Result:
- 1. Mobile phones
- 3. Audi
- 9. С200
- 10. X100
- 4. Ford
- 5. Mercedes
- 2. Cars
- 6. iPhone
- 7. Samsung
- 8. Motorola
Moving a node making it a new root
There is a special moveAsRoot() method that allows moving a node and making it
a new root. All descendants are moved as well in this case.
Example:
$node = Category::findOne(10); $node->moveAsRoot();
Recursive tree traversal
Category::find()->options(); // List all the tree Category::find()->options(1); // List all category in tree with root.id=1 Category::find()->options(1, 3); // List 3 levels of category in tree with root.id=1
Data format for Fancytree.
Category::find()->dataFancytree(); // List all the tree Category::find()->dataFancytree(1); // List all category in tree with root.id=1 Category::find()->dataFancytree(1, 3); // List 3 levels of category in tree with root.id=1
wbraganca/yii2-nested-set-behavior 适用场景与选型建议
wbraganca/yii2-nested-set-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.42k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2014 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「extension」 「nested set」 「yii2」 「yii2 behavior」 「yii2-nested-set-behavior」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wbraganca/yii2-nested-set-behavior 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wbraganca/yii2-nested-set-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wbraganca/yii2-nested-set-behavior 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple, immutable data structures
A custom URL rule class for Yii 2 which allows to create translated URL rules
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
Adds more BBCode
统计信息
- 总下载量: 9.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 17
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-11-09