将评论与自己联系起来

Relate Comments with themselves

我是 cakePHP 和整个 MVC 架构的新手。

我想建立一个类似 Reddit 的评论系统,其中有一条评论 hasMany 条评论,还有一条评论 belongsTo(仅)一条评论。

这是通常的评论部分,其中一条评论可以对该评论进行回复,回复可以有自己的回复等:

Nice article!
|
-- I didn't like it!
   |
   -- Well, you should have!
   |
   -- Yeah me neither!

现在我只设置了这样的'hasMany'关系:

class Comment extends AppModel {
    public $hasMany = array(
        'Comment' => array(
            'className' => 'Comment',
            'foreignKey' => 'comment_id',
            'dependent' => FALSE
        )
    );
}

comment_id 将包含父评论的 ID(如果评论没有父评论,则为 NULL)。

当我要求 cakePHP return 我所有属于特定 post 的评论时,它 return 给我这个(我手动添加了评论):

 Array
(
    [0] => Array
        (
            [Comment] => Array
                (
                    [id] => 2
                    [post_id] => 4
                    [comment_id] => 4
                    [body] => Child of 4
                    [created] => 2015-01-31 18:01:27
                    [modified] => 2015-01-31 18:01:27
                    [0] => Array
                        (
                            [id] => 3
                            [post_id] => 4
                            [comment_id] => 2
                            [body] => Child of 2
                            [created] => 2015-01-01 00:00:00
                            [modified] => 2015-01-16 00:00:00
                        )

                    [1] => Array
                        (
                            [id] => 5
                            [post_id] => 4
                            [comment_id] => 2
                            [body] => 2nd child of 2
                            [created] => 2015-01-14 00:00:00
                            [modified] => 2015-01-28 00:00:00
                        )

                )

        )

    [1] => Array
        (
            [Comment] => Array
                (
                    [id] => 3
                    [post_id] => 4
                    [comment_id] => 2
                    [body] => Child of 2
                    [created] => 2015-01-01 00:00:00
                    [modified] => 2015-01-16 00:00:00
                )

        )

    [2] => Array
        (
            [Comment] => Array
                (
                    [id] => 4
                    [post_id] => 4
                    [comment_id] => 
                    [body] => This is the parent of all
                    [created] => 2015-01-21 00:00:00
                    [modified] => 2015-01-30 00:00:00
                    [0] => Array
                        (
                            [id] => 2
                            [post_id] => 4
                            [comment_id] => 4
                            [body] => Child of 4
                            [created] => 2015-01-31 18:01:27
                            [modified] => 2015-01-31 18:01:27
                        )

                )

        )

    [3] => Array
        (
            [Comment] => Array
                (
                    [id] => 5
                    [post_id] => 4
                    [comment_id] => 2
                    [body] => 2nd child of 2
                    [created] => 2015-01-14 00:00:00
                    [modified] => 2015-01-28 00:00:00
                )

        )

)

据我了解,它会获取所有评论,然后针对每条评论重新搜索数据库中具有 'children'.

的评论

使 cakePHP return 成为一个 'ready' 注释数组的正确方法是什么:

Array
(
    [0] => Array
        (
            [Comment] => Array
                (
                    [id] => 4
                    [post_id] => 4
                    [comment_id] => 
                    [body] => This is the parent of all
                    [created] => 2015-01-21 00:00:00
                    [modified] => 2015-01-30 00:00:00
                    [0] => Array
                        (
                            [id] => 2
                            [post_id] => 4
                            [comment_id] => 4
                            [body] => Child of 4
                            [created] => 2015-01-31 18:01:27
                            [modified] => 2015-01-31 18:01:27
                            [0] => Array
                            (
                                [id] => 5
                                [post_id] => 4
                                [comment_id] => 2
                                [body] => 2nd child of 2
                                [created] => 2015-01-14 00:00:00
                                [modified] => 2015-01-28 00:00:00
                            )
                            [1] => Array
                            (
                                [id] => 5
                                [post_id] => 4
                                [comment_id] => 2
                                [body] => 2nd child of 2
                                [created] => 2015-01-14 00:00:00
                                [modified] => 2015-01-28 00:00:00
                            )

                        )
                )
        )
)

您可能想使用 find('threaded')。您还可以查看树的行为。