Download the PHP package jiaxincui/closure-table without Composer
On this page you can find all versions of the php package jiaxincui/closure-table. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jiaxincui/closure-table
More information about jiaxincui/closure-table
Files in jiaxincui/closure-table
Package closure-table
Short Description A closure table tree package for laravel eloquent
License MIT
Informations about the package closure-table
About
优雅的树形数据结构管理包,基于Closure Table
模式设计.
Features
- 优雅的树形数据设计模式
- 最少的数据库查询
- 一个Eloquent Trait简单操作
- 完善的树形结构操作方法
- 支持生成树形数据
- 支持多个根存在
- 支持节点/树修复
- 支持软删除
- ...
依赖
- php > 5.6.0
- laravel ~5.0|~6.0|~7.0|~8.0
- mysql > 5.1.0
关于Closure Table
Closure table is a simple and elegant way of storing and querying hierarchical data in any RDBMS. By hierarchical data we mean a set of data that has some parent – child relationship among them. We use the word ‘tree’ instead of hierarchies commonly. As an example we may take the relationships between geographic locations like ‘Countries’, ‘States/ Province’, ‘Districts/ Cities’ etc.
Closure Table
将树中每个节点与其后代节点的关系存储在数据库中,
这将需要一个存储节点关系的表xxx_closure
.
例如一个菜单表menus
:
id | name | parent |
---|---|---|
1 | A | 0 |
2 | AA | 1 |
3 | AB | 1 |
4 | AAA | 2 |
5 | ABA | 3 |
6 | ABAA | 5 |
一个基本的closure
表包含ancestor
,descendant
,distance
3个字段,如:
ancestor | descendant | distance |
---|---|---|
1 | 1 | 0 |
1 | 2 | 1 |
1 | 3 | 1 |
1 | 4 | 2 |
1 | 5 | 2 |
1 | 6 | 3 |
2 | 2 | 0 |
2 | 4 | 1 |
3 | 3 | 0 |
3 | 5 | 1 |
3 | 6 | 2 |
4 | 4 | 0 |
5 | 5 | 0 |
5 | 6 | 1 |
6 | 6 | 0 |
这个表记录了每个节点之间的关系,并且还记录了一条自身的关联,如:1,1,0
.
使用
ClosureTable
提供了大量方法操作树结构.
影响树结构的方法
它监听了
created
,updating
,restored
事件,如果你使用了create()
,或对实例使用了update(), restore()
它将自动维护树结构. 这意味着如果你在修改parent
列,它也会自动维护树结构.
获取数据的方法
-
以上
getXxx()
方法都包含一个query构造器,如getDescendants()
对应有一个queryDescendants()
,这使得你可以在查询中加入更多条件如:orderBy,
你可以这样使用
$menu->queryDescendants()->where('id', '>', 5)->orderBy('sort','desc')->get();
注意
getRoot()
,getParent()
,getRoots()
,getIsolated()
4个方法没有query构造器 -
如果你想获取只包含单个或多个字段的结果可以在
getXxx()
方法里传入参数,如:$menu->getAncestors(['id','name']);
生成树形数据的方法
提供多种方法生成树形数据,可从任意节点生成树
生成的树如下:
-
生成的树
children
键默认为children
,如果你想自定义可以作为第2个参数传入,如:$menu->getTree(['sortColumn', 'desc'], 'son');
如果你想获取只包含单个或多个列的结果可以作为第3个参数传入,如:
$menu->getTree(['sortColumn', 'desc'], 'son', ['id', 'name']);
- 你的表里可能包含多棵树,如果你想一一获取他们可以这样做:
判断方法
删除操作
ClosureTable
监听了模型的 deleting
事件
删除(包括软删除)一条记录,这个操作将解除自身的所有关联,
并且其所有 children
会成为根(parent = 0),这意味着所有的 children
成立了自己的树.
请勿使用以下方法来删除模型
Menu::destroy(1);
Menu::where('id', 1)->delete()
因为这些操作不会触发deleting
事件
支持软删除,软删除的
restore()
, 会根据它的parent
列的记录恢复至相应的位置,你不需要去关心closure
表里的记录,它已经帮你做了.
结构维护
安装
-
你的模型中必要的列
id
,parent
, closure
表必要的列ancestor
,descendant
,distance
示例:
-
在
model
里引入 traitJiaxincui\ClosureTable\Traits\ClosureTable
. -
(非必要)如果你想自定义关联表名和字段,可在
model
里定义以下属性:$closureTable
,$ancestorColumn
,$descendantColumn
,$distanceColumn
. -
(非必要)如果你想自定义
parent
字段,在model
里定义属性$parentColumn
.如下示例:
接下来,你就可以自由的使用ClosureTable
带来的所有功能了.
License
All versions of closure-table with dependencies
illuminate/support Version ^5.0|^6.0|^7.0|^8.0|^9.0