Laravelの階層ツリーコメント管理システム

ツリーのようなコメントの管理を整理できるLaravelフレームワークのパッケージ 階層構造を格納する2つの方法(「クロージャーテーブル」 と 「隣接リスト」)の共生が使用され ます。

要件

  • Laravel5.7以降のフレームワーク

  • PHPのバージョンは7.3以上です。

リポジトリ

https://github.com/drandin/closure-table-comments

利点

ClosureTableメソッドとAdjacencyListメソッドを組み合わせて使用​​すると、次のことが可能になります。

  • データベースクエリの数を最小限に抑えます。コメントスレッドを取得するには、1つのリクエストで十分です。

  • 高性能を提供します。データベースへの読み取り要求は単純であり、階層ノードの選択速度は、データ量の増加に伴って実際には低下しません。

  • コメントテキストと階層構造を別々に2つのテーブルに保存します。

  • コメントのネストの深さを制御します。階層内のノードのレベルは常にわかっているため、ノードを判別するためにノードの祖先に関する情報を取得する必要はありません。

  • 階層のデータ整合性を確保します。階層に新しいノードを追加するために、以前に作成したノードのリンクを変更する必要はありません。

  • 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»

 «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» , .

, . 

, , , , . 

, . , , , . 

, , , .

  1. «Closure Table» «Adjacency List» https://habr.com/ru/post/263629/. 2015 .

  2. «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.

  3. (Bill Karwin). http://www.slideshare.net/billkarwin/models-for-hierarchical-data.

  4. データベースにツリーを保存します。パート1、理論的https://habr.com/ru/post/193166/2013年。




All Articles