creocoder/yii2-nested-sets
Composer 安装命令:
composer require creocoder/yii2-nested-sets
包简介
The nested sets behavior for the Yii framework
关键字:
README 文档
README
A modern nested sets behavior for the Yii framework utilizing the Modified Preorder Tree Traversal algorithm.
Installation
The preferred way to install this extension is through composer.
Either run
$ composer require creocoder/yii2-nested-sets
or add
"creocoder/yii2-nested-sets": "0.9.*"
to the require section of your composer.json file.
Migrations
Run the following command
$ yii migrate/create create_menu_table
Open the /path/to/migrations/m_xxxxxx_xxxxxx_create_menu_table.php file,
inside the up() method add the following
$this->createTable('{{%menu}}', [ 'id' => $this->primaryKey(), //'tree' => $this->integer()->notNull(), 'lft' => $this->integer()->notNull(), 'rgt' => $this->integer()->notNull(), 'depth' => $this->integer()->notNull(), 'name' => $this->string()->notNull(), ]);
To use multiple tree mode uncomment tree field.
Configuring
Configure model as follows
use creocoder\nestedsets\NestedSetsBehavior; class Menu extends \yii\db\ActiveRecord { public function behaviors() { return [ 'tree' => [ 'class' => NestedSetsBehavior::className(), // 'treeAttribute' => 'tree', // 'leftAttribute' => 'lft', // 'rightAttribute' => 'rgt', // 'depthAttribute' => 'depth', ], ]; } public function transactions() { return [ self::SCENARIO_DEFAULT => self::OP_ALL, ]; } public static function find() { return new MenuQuery(get_called_class()); } }
To use multiple tree mode uncomment treeAttribute array key inside behaviors() method.
Configure query class as follows
use creocoder\nestedsets\NestedSetsQueryBehavior; class MenuQuery extends \yii\db\ActiveQuery { public function behaviors() { return [ NestedSetsQueryBehavior::className(), ]; } }
Usage
Making a root node
To make a root node
$countries = new Menu(['name' => 'Countries']); $countries->makeRoot();
The tree will look like this
- Countries
Prepending a node as the first child of another node
To prepend a node as the first child of another node
$russia = new Menu(['name' => 'Russia']); $russia->prependTo($countries);
The tree will look like this
- Countries
- Russia
Appending a node as the last child of another node
To append a node as the last child of another node
$australia = new Menu(['name' => 'Australia']); $australia->appendTo($countries);
The tree will look like this
- Countries
- Russia
- Australia
Inserting a node before another node
To insert a node before another node
$newZeeland = new Menu(['name' => 'New Zeeland']); $newZeeland->insertBefore($australia);
The tree will look like this
- Countries
- Russia
- New Zeeland
- Australia
Inserting a node after another node
To insert a node after another node
$unitedStates = new Menu(['name' => 'United States']); $unitedStates->insertAfter($australia);
The tree will look like this
- Countries
- Russia
- New Zeeland
- Australia
- United States
Getting the root nodes
To get all the root nodes
$roots = Menu::find()->roots()->all();
Getting the leaves nodes
To get all the leaves nodes
$leaves = Menu::find()->leaves()->all();
To get all the leaves of a node
$countries = Menu::findOne(['name' => 'Countries']); $leaves = $countries->leaves()->all();
Getting children of a node
To get all the children of a node
$countries = Menu::findOne(['name' => 'Countries']); $children = $countries->children()->all();
To get the first level children of a node
$countries = Menu::findOne(['name' => 'Countries']); $children = $countries->children(1)->all();
Getting parents of a node
To get all the parents of a node
$countries = Menu::findOne(['name' => 'Countries']); $parents = $countries->parents()->all();
To get the first parent of a node
$countries = Menu::findOne(['name' => 'Countries']); $parent = $countries->parents(1)->one();
Donating
Support this project and others by creocoder via gratipay.
creocoder/yii2-nested-sets 适用场景与选型建议
creocoder/yii2-nested-sets 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.93M 次下载、GitHub Stars 达 444, 最近一次更新时间为 2015 年 01 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「nested sets」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 creocoder/yii2-nested-sets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 creocoder/yii2-nested-sets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 creocoder/yii2-nested-sets 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
Simple minimal but useful set of utils (properties, strings, datetimes, etc.)
Nested Set Model for Laravel 12+ (Lunar-maintained fork of kalnoy/nestedset)
Baum is an implementation of the Nested Set pattern for Eloquent models.
Nested field validation for Yii2
Baum is an implementation of the Nested Set pattern for Eloquent models.
统计信息
- 总下载量: 4.93M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 449
- 点击次数: 18
- 依赖项目数: 102
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-01-01