paolzi / yii2-materialized-path
Yii2 的 Materialized Path 行为
v2.1.1
2020-12-04 15:27 UTC
Requires
- php: >=5.4.0
- paulzi/yii2-sortable: ~1.0
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- phpunit/dbunit: ~1.0
- phpunit/phpunit: ~4.0
README
实现将树存储在数据库表中的物化路径算法。
安装
通过 Composer 安装
composer require paulzi/yii2-materialized-path
或添加
"paulzi/yii2-materialized-path" : "^2.1"
到您的 composer.json
文件的 require
部分。
迁移示例
单个树迁移
class m150828_150000_single_tree extends Migration { public function up() { $tableOptions = null; if ($this->db->driverName === 'mysql') { // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; } $this->createTable('{{%single_tree}}', [ 'id' => Schema::TYPE_PK, 'path' => Schema::TYPE_STRING . ' NULL', 'depth' => Schema::TYPE_INTEGER . ' NOT NULL', 'sort' => Schema::TYPE_INTEGER . ' NOT NULL', 'name' => Schema::TYPE_STRING . ' NOT NULL', // example field ], $tableOptions); $this->createIndex('path', '{{%single_tree}}', ['path']); } }
多个树迁移
class m150828_150100_multiple_tree extends Migration { public function up() { $tableOptions = null; if ($this->db->driverName === 'mysql') { // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; } $this->createTable('{{%multiple_tree}}', [ 'id' => Schema::TYPE_PK, 'tree' => Schema::TYPE_INTEGER . ' NULL', 'path' => Schema::TYPE_STRING . ' NULL', 'depth' => Schema::TYPE_INTEGER . ' NOT NULL', 'sort' => Schema::TYPE_INTEGER . ' NOT NULL', 'name' => Schema::TYPE_STRING . ' NOT NULL', // example field ], $tableOptions); $this->createIndex('path', '{{%multiple_tree}}', ['tree', 'path']); } }
配置
use paulzi\materializedPath\MaterializedPathBehavior; class Sample extends \yii\db\ActiveRecord { public function behaviors() { return [ [ 'class' => MaterializedPathBehavior::className(), // 'treeAttribute' => 'tree', ], ]; } public function transactions() { return [ self::SCENARIO_DEFAULT => self::OP_ALL, ]; } }
可选地,您可以设置查询以查找根节点
class Sample extends \yii\db\ActiveRecord { public static function find() { return new SampleQuery(get_called_class()); } }
查询类
use paulzi\materializedPath\MaterializedPathQueryTrait; class SampleQuery extends \yii\db\ActiveQuery { use MaterializedPathQueryTrait; }
可排序行为
此行为附加了 SortableBehavior。您可以使用其方法(例如,reorder())。
选项
$pathAttribute = 'path'
- 在表模式中设置路径属性。$depthAttribute = 'depth'
- 在表模式中设置深度属性。$itemAttribute = null
- 在表模式中设置项属性以获取路径,如果未设置值,则使用主键。$treeAttribute = null
- 对于多树,当项属性不是主键时,设置树属性。$sortable = []
- SortableBehavior 设置 - 查看 paulzi/yii2-sortable。$delimiter = '/'
- 路径项的分隔符。$rootDepthValue = 0
- 设置根节点$depthAttribute
的值。
用法
选择
获取根节点
如果您连接 MaterializedPathQueryTrait
,则可以获取所有根节点
$roots = Sample::find()->roots()->all();
获取节点的祖先
获取节点祖先的方法
$node11 = Sample::findOne(['name' => 'node 1.1']); $parents = $node11->parents; // via relation $parents = $node11->getParents()->all(); // via query $parents = $node11->getParents(2)->all(); // get 2 levels of ancestors
获取节点父节点的方法
$node11 = Sample::findOne(['name' => 'node 1.1']); $parent = $node11->parent; // via relation $parent = $node11->getParent()->one(); // via query
获取节点根节点的方法
$node11 = Sample::findOne(['name' => 'node 1.1']); $root = $node11->root; // via relation $root = $node11->getRoot()->one(); // via query
获取节点的后代
获取节点所有后代的方法
$node11 = Sample::findOne(['name' => 'node 1.1']); $descendants = $node11->descendants; // via relation $descendants = $node11->getDescendants()->all(); // via query $descendants = $node11->getDescendants(2, true)->all(); // get 2 levels of descendants and self node
填充节点的 children
关系,包括节点及其后代
$node11 = Sample::findOne(['name' => 'node 1.1']); $tree = $node11->populateTree(); // populate all levels $tree = $node11->populateTree(2); // populate 2 levels of descendants
获取节点子节点的方法
$node11 = Sample::findOne(['name' => 'node 1.1']); $children = $node11->children; // via relation $children = $node11->getChildren()->all(); // via query
获取叶节点
获取节点所有叶的方法
$node11 = Sample::findOne(['name' => 'node 1.1']); $leaves = $node11->leaves; // via relation $leaves = $node11->getLeaves(2)->all(); // get 2 levels of leaves via query
获取相邻节点
获取下一个节点的方法
$node11 = Sample::findOne(['name' => 'node 1.1']); $next = $node11->next; // via relation $next = $node11->getNext()->one(); // via query
获取上一个节点的方法
$node11 = Sample::findOne(['name' => 'node 1.1']); $prev = $node11->prev; // via relation $prev = $node11->getPrev()->one(); // via query
一些检查
$node1 = Sample::findOne(['name' => 'node 1']); $node11 = Sample::findOne(['name' => 'node 1.1']); $node11->isRoot() - return true, if node is root $node11->isLeaf() - return true, if node is leaf $node11->isChildOf($node1) - return true, if node11 is child of $node1
修改
创建根节点的方法
$node11 = new Sample(); $node11->name = 'node 1.1'; $node11->makeRoot()->save();
注意:如果您允许多个树并且属性 tree
未设置,则自动采用主键值。
将节点作为另一个节点的第一个子节点添加的方法
$node1 = Sample::findOne(['name' => 'node 1']); $node11 = new Sample(); $node11->name = 'node 1.1'; $node11->prependTo($node1)->save(); // inserting new node
将节点作为另一个节点的最后一个子节点添加的方法
$node11 = Sample::findOne(['name' => 'node 1.1']); $node12 = Sample::findOne(['name' => 'node 1.2']); $node12->appendTo($node11)->save(); // move existing node
在另一个节点之前插入节点的方法
$node13 = Sample::findOne(['name' => 'node 1.3']); $node12 = new Sample(); $node12->name = 'node 1.2'; $node12->insertBefore($node13)->save(); // inserting new node
在另一个节点之后插入节点的方法
$node13 = Sample::findOne(['name' => 'node 1.3']); $node14 = Sample::findOne(['name' => 'node 1.4']); $node14->insertAfter($node13)->save(); // move existing node
删除具有后代的节点的方法
$node11 = Sample::findOne(['name' => 'node 1.1']); $node11->delete(); // delete node, children come up to the parent $node11->deleteWithChildren(); // delete node and all descendants
重新排序子节点
$model = Sample::findOne(1); $model->reorderChildren(true); // reorder with center zero $model = Sample::findOne(2); $model->reorderChildren(false); // reorder from zero
从 1.x 升级到 2.x
- 将
sortAttribute
和step
属性移动到sortable
属性中。 - 将命名空间从
paulzi\materializedpath
更改为paulzi\materializedPath
。 - 包含
paulzi\yii2-sortable
(运行composer update
)。