Um pacote para o framework Laravel que permite organizar o gerenciamento de comentários em árvore. É usada uma simbiose de dois métodos de armazenamento de estruturas hierárquicas - "Tabela de Fechamento" e "Lista de Adjacências" .
Requisitos
Laravel 5.7 ou framework mais recente.
A versão do PHP é pelo menos 7.3.
Repositório
https://github.com/drandin/closure-table-comments
Benefícios
O uso combinado dos métodos Tabela de Fechamento e Lista de Adjacência permite:
Minimize o número de consultas ao banco de dados. Um pedido é suficiente para recuperar um tópico de comentário.
Fornece alto desempenho. Os pedidos de leitura ao banco de dados são simples e a velocidade de seleção dos nós da hierarquia praticamente não diminui com o aumento da quantidade de dados.
Armazene o texto do comentário e a estrutura hierárquica separadamente, em duas tabelas.
Controle a profundidade do aninhamento de comentários. O nível dos nós na hierarquia é sempre conhecido, não há necessidade de obter informações sobre os ancestrais de um nó para determiná-lo.
Garanta a integridade dos dados da hierarquia. Adicionar novos nós à hierarquia não requer nenhuma alteração nos links dos nós criados anteriormente.
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.
Armazenando árvores no banco de dados. Parte um, teórica https://habr.com/ru/post/193166/ . ano 2013.