CakePHP 3 中递归的替代方案6.x

Alternative for Recursive in CakePHP 3.6.x

在 CakePHP 2.x 中有递归设置,根据设置,它的查询会深入 X 级...

我看了下面的问题,但是他们没有回答我的问题...


现在,我正在尝试递归检索以下 table

+----+------------+------------+------------+------------------------------------------+
| id | message_id | account_id | to_user_id | message                                  |
+----+------------+------------+------------+------------------------------------------+
|  1 |          0 | 10         |       1617 | asd a sfg sdfhgdfh  gf dgh dfghfdgh      |
|  2 |          1 | 1617       |         10 | afdkjnjdsf ndfs jfsd kjlfdgs kljgfsd jkl |
|  3 |          2 | 10         |       1617 | t5ge6h65h 6e6heh6eh6 he h e              |
|  4 |          3 | 1617       |         10 | kljnnjkl nkl njkln jkln jkl              |
|  5 |          4 | 10         |       1617 | fffffffffffffff                          |
+----+------------+------------+------------+------------------------------------------+

具有以下table定义

$this->belongsTo('Messages', [
    'foreignKey' => 'message_id',
    'joinType' => 'INNER'
]);
$this->hasMany('Replies', [
    'className' => 'Messages',
    'foreignKey' => 'message_id',
    'propertyName' => 'replies'
]);

和下面的查找语句

$message = $this->Messages->find('all', ['contain' => ['Replies']])->where(['Messages.id' => $id])->first();

这些是我的结果

App\Model\Entity\Message Object
(
    [id] => 1
    [account_id] => 10
    [message] => asd a sfg sdfhgdfh  gf dgh dfghfdgh
    [message_id] => 0
    [to_user_id] => 1617
    [replies] => Array
        (
            [0] => App\Model\Entity\Message Object
                (
                    [id] => 2
                    [account_id] => 1617
                    [message] => afdkjnjdsf ndfs jfsd kjlfdgs kljgfsd jkl
                    [message_id] => 1
                    [to_user_id] => 10
                    [[new]] => 
                    [[accessible]] => Array
                        (
                            [account_id] => 1
                            [message] => 1
                            [message_id] => 1
                            [to_user_id] => 1
                            [messages] => 1
                        )

                    [[dirty]] => Array
                        (
                        )

                    [[original]] => Array
                        (
                        )

                    [[virtual]] => Array
                        (
                        )

                    [[errors]] => Array
                        (
                        )

                    [[invalid]] => Array
                        (
                        )

                    [[repository]] => Replies
                )

        )
    [[new]] => 
    [[accessible]] => Array
        (
            [account_id] => 1
            [message] => 1
            [message_id] => 1
            [to_user_id] => 1
            [messages] => 1
        )

    [[dirty]] => Array
        (
        )

    [[original]] => Array
        (
        )

    [[virtual]] => Array
        (
        )

    [[errors]] => Array
        (
        )

    [[invalid]] => Array
        (
        )

    [[repository]] => Messages
)

我的问题是,如何获取要填充的 "replies" 数组项的 "Replies" 属性?即:如何在回复中填充第二个和第三个等级别的回复?

您必须明确说明您现在想要包含的内容。要获得三个级别的回复,这应该有效:

$message = $this->Messages->find('all', [
    'contain' => ['Replies' => ['Replies' => ['Replies']]]
])->where(['Messages.id' => $id])->first();