sivanandaperumal/closure-table
Composer 安装命令:
composer require sivanandaperumal/closure-table
包简介
Adjacency List’ed Closure Table database design pattern implementation for Laravel. Includes restore of tree
README 文档
README
Branches
- L4 supports Laravel 4
- L5.1 supports Laravel < 5.2
- L5.3 supports Laravel 5.2-5.3
- L5.4 supports Laravel 5.4
- master is for any actual Laravel version, so be careful
Hi, this is a database package for Laravel. It's intended to use when you need to operate hierarchical data in database. The package is an implementation of a well-known database design pattern called Closure Table. The package includes generators for models and migrations.
Installation
To install the package, put the following in your composer.json:
"require": { "franzose/closure-table": "4.*" }
And to app/config/app.php:
'providers' => array( // ... 'Franzose\ClosureTable\ClosureTableServiceProvider', ),
Setup your ClosureTable
Create models and migrations
For example, let's assume you're working on pages. You can just use an artisan command to create models and migrations automatically without preparing all the stuff by hand. Open terminal and put the following:
php artisan closuretable:make --entity=page
All options of the command:
--namespace,-ns[optional]: namespace for classes, set by--entityand--closureoptions, helps to avoid namespace duplication in those options--entity,-e: entity class name; if namespaced name is used, then the default closure class name will be prepended with that namespace--entity-table,-et[optional]: entity table name--closure,-c[optional]: closure class name--closure-table[optional],-ct: closure table name--models-path,-mdl[optional]: custom models path--migrations-path,-mgr[optional]: custom migrations path--use-innodband-i[optional]: InnoDB migrations have been made optional as well with new paramaters. Setting this will enable the InnoDB engine.
That's almost all, folks! The ‘dummy’ stuff has just been created for you. You will need to add some fields to your entity migration because the created ‘dummy’ includes just required id, parent_id, position, and real depth columns:
idis a regular autoincremented columnparent_idcolumn is used to simplify immediate ancestor querying and, for example, to simplify building the whole treepositioncolumn is used widely by the package to make entities sortablereal depthcolumn is also used to simplify queries and reduce their number
By default, entity’s closure table includes the following columns:
- Autoincremented identifier
- Ancestor column points on a parent node
- Descendant column points on a child node
- Depth column shows a node depth in the tree
It is by closure table pattern design, so remember that you must not delete these four columns.
Remember that many things are made customizable, so see ‘Customization’ for more information.
Time of coding
Once your models and their database tables are created, at last, you can start actually coding. Here I will show you ClosureTable's specific approaches.
Direct ancestor (parent)
$parent = Page::find(15)->getParent();
Ancestors
$page = Page::find(15); $ancestors = $page->getAncestors(); $ancestors = $page->getAncestorsTree(); // Tree structure $ancestors = $page->getAncestorsWhere('position', '=', 1); $hasAncestors = $page->hasAncestors(); $ancestorsNumber = $page->countAncestors();
Direct descendants (children)
$page = Page::find(15); $children = $page->getChildren(); $hasChildren = $page->hasChildren(); $childrenNumber = $page->countChildren(); $newChild = new Page(array( 'title' => 'The title', 'excerpt' => 'The excerpt', 'content' => 'The content of a child' )); $newChild2 = new Page(array( 'title' => 'The title', 'excerpt' => 'The excerpt', 'content' => 'The content of a child' )); $page->addChild($newChild); //you can set child position $page->addChild($newChild, 5); //you can get the child $child = $page->addChild($newChild, null, true); $page->addChildren([$newChild, $newChild2]); $page->getChildAt(5); $page->getFirstChild(); $page->getLastChild(); $page->getChildrenRange(0, 2); $page->removeChild(0); $page->removeChild(0, true); //force delete $page->removeChildren(0, 3); $page->removeChildren(0, 3, true); //force delete
Descendants
$page = Page::find(15); $descendants = $page->getDescendants(); $descendants = $page->getDescendantsWhere('position', '=', 1); $descendantsTree = $page->getDescendantsTree(); $hasDescendants = $page->hasDescendants(); $descendantsNumber = $page->countDescendants();
Siblings
$page = Page::find(15); $first = $page->getFirstSibling(); //or $page->getSiblingAt(0); $last = $page->getLastSibling(); $atpos = $page->getSiblingAt(5); $prevOne = $page->getPrevSibling(); $prevAll = $page->getPrevSiblings(); $hasPrevs = $page->hasPrevSiblings(); $prevsNumber = $page->countPrevSiblings(); $nextOne = $page->getNextSibling(); $nextAll = $page->getNextSiblings(); $hasNext = $page->hasNextSiblings(); $nextNumber = $page->countNextSiblings(); //in both directions $hasSiblings = $page->hasSiblings(); $siblingsNumber = $page->countSiblings(); $sibligns = $page->getSiblingsRange(0, 2); $page->addSibling(new Page); $page->addSibling(new Page, 3); //third position //add and get the sibling $sibling = $page->addSibling(new Page, null, true); $page->addSiblings([new Page, new Page]); $page->addSiblings([new Page, new Page], 5); //insert from fifth position
Roots (entities that have no ancestors)
$roots = Page::getRoots(); $isRoot = Page::find(23)->isRoot(); Page::find(11)->makeRoot(0); //at the moment we always have to set a position when making node a root
Entire tree
$tree = Page::getTree(); $treeByCondition = Page::getTreeWhere('position', '>=', 1);
You deal with the collection, thus you can control its items as you usually do. Descendants? They are already loaded.
$tree = Page::getTree(); $page = $tree->find(15); $children = $page->getChildren(); $child = $page->getChildAt(3); $grandchildren = $page->getChildAt(3)->getChildren(); //and so on
Moving
$page = Page::find(25); $page->moveTo(0, Page::find(14)); $page->moveTo(0, 14);
Deleting subtree
If you don't use foreign keys for some reason, you can delete subtree manually. This will delete the page and all its descendants:
$page = Page::find(34); $page->deleteSubtree(); $page->deleteSubtree(true); //with subtree ancestor $page->deleteSubtree(false, true); //without subtree ancestor and force delete
Customization
You can customize default things in your own classes created by the ClosureTable artisan command:
- Entity table name: change
protected $tableproperty - Closure table name: do the same in your
ClosureTable(e.g.PageClosure) - Entity's
parent_id,position, andreal depthcolumn names: change return values ofgetParentIdColumn(),getPositionColumn(), andgetRealDepthColumn()respectively - Closure table's
ancestor,descendant, anddepthcolumns names: change return values ofgetAncestorColumn(),getDescendantColumn(), andgetDepthColumn()respectively.
sivanandaperumal/closure-table 适用场景与选型建议
sivanandaperumal/closure-table 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.84k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「categories」 「laravel」 「pages」 「hierarchy」 「closure table」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sivanandaperumal/closure-table 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sivanandaperumal/closure-table 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sivanandaperumal/closure-table 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Categories for NetCommons Plugin
PHP Interface for Babel Street Text Analytics
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 14.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-08