Laravel框架的软件包 ,使您可以组织对树状注释的管理。使用了两种存储分层结构的方法的共生关系: “关闭表” 和 “邻接表”。
要求
Laravel 5.7或更高版本的框架。
PHP版本至少为7.3。
资料库
https://github.com/drandin/closure-table-comments
好处
闭包表和邻接表方法的组合使用允许:
最小化数据库查询的数量。一个请求足以检索注释线程。
提供高性能。对数据库的读取请求很简单,并且层次结构节点的选择速度实际上不会随着数据量的增加而降低。
将注释文本和层次结构彼此分开存储在两个表中。
控制注释的嵌套深度。层次结构中的节点级别始终是已知的,因此无需获取有关节点祖先的信息即可确定它。
确保层次结构的数据完整性。将新节点添加到层次结构不需要更改先前创建的节点的链接。
SQL-. , , .
. .
«Adjacency List», , .
«Path Enumeration», «Materialized Path» - SQL- SELECT
, LIKE
: '%/2/3/4%'.
, .
— «Nested Sets». , . . «Nested Sets».
«Closure Table» , «» — .
«Closure Table» «Adjacency List» .
«Closure Table» «Adjacency List»:
«Closure Table» .
«Adjacency List» .
, , .
1. Laravel :
composer require drandin/closure-table-comments
2. config/app.php -. 'providers'.
\Drandin\ClosureTableComments\ClosureTableServiceProvider::class,
3. , closure-table-comments.php config :
php artisan vendor:publish --tag=config
, config/closure-table-comments.php . , , .
4. , :
php artisan config:cache
5. . , :
php artisan migrate
2 .
, .
, , «» , .
Comment StructureTree.
subject_id — «». NULL.
subject_id NULL, - .
user_id — « ». NULL.
user_id NULL, - . .
1. .
, «» 5636 7 .
use Drandin\ClosureTableComments\ClosureTableService;
use Drandin\ClosureTableComments\Commentator;
$commentator = new Commentator(new ClosureTableService());
$comment = " . .";
$id = $commentator
->setSubjectId(5636)
->addCommentToRoot($comment, 7);
$id, 5636. 7.
2. , .
, «» 5636 43 .
( Node), . , 1.
use Drandin\ClosureTableComments\ClosureTableService;
use Drandin\ClosureTableComments\Commentator;
$commentator = new Commentator(new ClosureTableService());
$comment = " , .";
$id = $commentator
->setSubjectId(5636)
->replyToComment(1, $comment, 43);
$id, 5636. 43.
, ( 1). , (level) , .
3. .
.
use Drandin\ClosureTableComments\ClosureTableService;
use Drandin\ClosureTableComments\Commentator;
$commentator = new Commentator(new ClosureTableService());
$comment = " . .";
$res = $commentator->editComment(1, $comment);
, $res true.
4. .
( ) .
use Drandin\ClosureTableComments\ClosureTableService;
use Drandin\ClosureTableComments\Commentator;
$commentator = new Commentator(new ClosureTableService());
$res = $commentator->has(2);
2 , $res true.
5. ( ) .
, Node , 2.
use Drandin\ClosureTableComments\ClosureTableService;
use Drandin\ClosureTableComments\Commentator;
$commentator = new Commentator(new ClosureTableService());
$node = $commentator->getNode(2);
, 2 , getNode Node. Node .
6. .
5636, . . , .
, . . . , , .
getTreeBranch.
use Drandin\ClosureTableComments\ClosureTableService;
use Drandin\ClosureTableComments\Commentator;
$commentator = new Commentator(new ClosureTableService());
$nodes = $commentator
->setSubjectId(5636)
->getTreeBranch();
Node.
, getTreeBranch .
use Drandin\ClosureTableComments\ClosureTableService;
use Drandin\ClosureTableComments\Commentator;
$commentator = new Commentator(new ClosureTableService());
$nodes = $commentator
->setSubjectId(5636)
->getTreeBranch(2);
2.
7. .
getTreeBranchArray.
use Drandin\ClosureTableComments\ClosureTableService;
use Drandin\ClosureTableComments\Commentator;
$commentator = new Commentator(new ClosureTableService());
$tree = $commentator
->setSubjectId(5636)
->getTreeBranchArray();
, , getTreeBranchArray .
8. .
, 23.
use Drandin\ClosureTableComments\ClosureTableService;
use Drandin\ClosureTableComments\Commentator;
$commentator = new Commentator(new ClosureTableService());
$ids = $commentator->getBranchIds(23);
$ids .
9. .
, 23 , Node getNode , . , getLevel.
use Drandin\ClosureTableComments\ClosureTableService;
use Drandin\ClosureTableComments\Commentator;
$commentator = new Commentator(new ClosureTableService());
$level = $commentator->getLevel(23);
10. () .
( ), delete.
delete , , .
64 , .
use Drandin\ClosureTableComments\ClosureTableService;
use Drandin\ClosureTableComments\Commentator;
$commentator = new Commentator(new ClosureTableService());
$res = $commentator->delete(64);
, .
«Closure Table»
«Closure Table» , , . , . , , .
, , «Closure Table» , .
, .
, , , , .
, . , , , .
, , , .
«Closure Table» «Adjacency List» https://habr.com/ru/post/263629/. 2015 .
«Stack Overflow». MySQL Closure Table hierarchical ? http://stackoverflow.com/questions/8252323/mysql-closure-table-hierarchical-database-how-to-pull-information-out-in-the-c, ? http://stackoverflow.com/questions/192220/what-is-the-most-efficient-elegant-way-to-parse-a-flat-table-into-a-tree.
(Bill Karwin). http://www.slideshare.net/billkarwin/models-for-hierarchical-data.
在数据库中存储树。第一部分,理论上的https://habr.com/ru/post/193166/。2013年。