CakePHP 3.6.x中的递归替代方法

时间:2018-12-02 12:54:19

标签: php cakephp cakephp-3.0 cakephp-3.6

在CakePHP 2.x中,有一个递归设置,根据设置,它的查询将达到X级别的深度...

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

CakePHP 3 Fetch associated data recursive

Recursive in cakephp3?


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

+----+------------+------------+------------+------------------------------------------+
| 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                          |
+----+------------+------------+------------+------------------------------------------+

具有以下表格定义

$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”属性?即:如何获得第二和第三等级别的答复要填充到答复中?

1 个答案:

答案 0 :(得分:1)

您必须明确声明要包含的内容。要获得三个级别的答复,这应该可行:

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