Paket untuk kerangka kerja Laravel yang memungkinkan Anda mengatur pengelolaan komentar mirip pohon. Simbiosis dari dua metode penyimpanan struktur hierarki digunakan - "Tabel Penutupan" dan "Daftar Kedekatan" .
Persyaratan
Laravel 5.7 atau kerangka kerja yang lebih baru.
Versi PHP setidaknya 7.3.
Gudang
https://github.com/drandin/closure-table-comments
Manfaat
Penggunaan gabungan metode Tabel Penutupan dan Daftar Kedekatan memungkinkan:
Minimalkan jumlah kueri database. Satu permintaan cukup untuk mengambil untaian komentar.
Memberikan kinerja tinggi. Permintaan baca ke database sederhana dan kecepatan pemilihan node hierarki secara praktis tidak berkurang seiring dengan peningkatan jumlah data.
Simpan teks komentar dan struktur hierarki secara terpisah satu sama lain, dalam dua tabel.
Kontrol kedalaman penumpukan komentar. Tingkat node dalam hierarki selalu diketahui; tidak perlu mendapatkan informasi tentang nenek moyang node untuk menentukannya.
Pastikan integritas data dari hierarki. Menambahkan node baru ke hierarki tidak memerlukan perubahan apa pun pada link dari node yang dibuat sebelumnya.
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.
Menyimpan pohon di database. Bagian satu, teoritis https://habr.com/ru/post/193166/ . tahun 2013.