在CakePHP中使用read函数检索递归数据

在CakePHP中使用read函数检索递归数据,cakephp,cakephp-2.0,cakephp-2.2,Cakephp,Cakephp 2.0,Cakephp 2.2,我有一个类似博客的系统。每个条目都可以有注释。每个注释都是由用户创建的 我目前正在控制器上的“查看”操作中使用读取功能来检索所有数据 模型之间的关系已经创建(belongTo、hasMany…等) 当调用entry视图时,我得到如下结果: ['Entry'] => Array ( [id] => 1 [body] => 'xxxxxx' [...] => ... ) [Comment] => Arra

我有一个类似博客的系统。每个条目都可以有注释。每个注释都是由用户创建的

我目前正在控制器上的“查看”操作中使用读取功能来检索所有数据

模型之间的关系已经创建(belongTo、hasMany…等)

当调用entry视图时,我得到如下结果:

['Entry'] => Array
    (
        [id] => 1
        [body] => 'xxxxxx'
        [...] => ...
    )

[Comment] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 1
                [body] => This is an example of a comment guys!
                [created] => 0000-00-00 00:00:00
            )

        [1] => Array
            (
                [id] => 2
                [user_id] => 1
                [body] => This is the 2nd comment!!!
                [created] => 0000-00-00 00:00:00
            )
    )
['Entry'] => Array
    (
        [id] => 1
        [body] => xxxxxx
        [...] => ...
    )

[Comment] => Array
    (
        [0] => Array
            (
              [Comment] => Array
                   (
                      [id] => 1
                      [user_id] => 1
                      [body] => This is an example of a comment guys!
                      [created] => 0000-00-00 00:00:00  
                   )

              [User] => Array
                   (
                      [id] => 1
                      [username] => myusername
                      [created] => 0000-00-00 00:00:00  
                   )
            )

        [1] => Array
            (
              [Comment] => Array
                   (
                      [id] => 1
                      [user_id] => 2
                      [body] => fasdfasfaT
                      [created] => 0000-00-00 00:00:00  
                   )

              [User] => Array
                   (
                      [id] => 2
                      [username] => myusername2
                      [created] => 0000-00-00 00:00:00  
                   )
            )
    )
$this->Entry->find('all', array(
    ...
    'contain' => array('Comment' => 'User')
));
['Entry'] => Array
(
    [id] => 1
    [body] => xxxxxx
    [...] => ...
)

[Comment] => Array
(
    [0] => Array
        (
          [id] => 1
          [user_id] => 1
          [body] => This is an example of a comment guys!
          [created] => 0000-00-00 00:00:00  
          [User] => Array
               (
                  [id] => 1
                  [username] => myusername
                  [created] => 0000-00-00 00:00:00  
               )
        )

)
使用read函数,是否有任何方法也可以检索注释的“递归”数据,例如与用户id相关的用户数据?(为了得到他们的名字等)

我期待这样的事情:

['Entry'] => Array
    (
        [id] => 1
        [body] => 'xxxxxx'
        [...] => ...
    )

[Comment] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 1
                [body] => This is an example of a comment guys!
                [created] => 0000-00-00 00:00:00
            )

        [1] => Array
            (
                [id] => 2
                [user_id] => 1
                [body] => This is the 2nd comment!!!
                [created] => 0000-00-00 00:00:00
            )
    )
['Entry'] => Array
    (
        [id] => 1
        [body] => xxxxxx
        [...] => ...
    )

[Comment] => Array
    (
        [0] => Array
            (
              [Comment] => Array
                   (
                      [id] => 1
                      [user_id] => 1
                      [body] => This is an example of a comment guys!
                      [created] => 0000-00-00 00:00:00  
                   )

              [User] => Array
                   (
                      [id] => 1
                      [username] => myusername
                      [created] => 0000-00-00 00:00:00  
                   )
            )

        [1] => Array
            (
              [Comment] => Array
                   (
                      [id] => 1
                      [user_id] => 2
                      [body] => fasdfasfaT
                      [created] => 0000-00-00 00:00:00  
                   )

              [User] => Array
                   (
                      [id] => 2
                      [username] => myusername2
                      [created] => 0000-00-00 00:00:00  
                   )
            )
    )
$this->Entry->find('all', array(
    ...
    'contain' => array('Comment' => 'User')
));
['Entry'] => Array
(
    [id] => 1
    [body] => xxxxxx
    [...] => ...
)

[Comment] => Array
(
    [0] => Array
        (
          [id] => 1
          [user_id] => 1
          [body] => This is an example of a comment guys!
          [created] => 0000-00-00 00:00:00  
          [User] => Array
               (
                  [id] => 1
                  [username] => myusername
                  [created] => 0000-00-00 00:00:00  
               )
        )

)
谢谢。

是的,是的

您可以通过属性控制关联的深度,或者更确切地说,使用行为来指定要包含的模型。我总是对
AppModel.php
中的所有模型启用此行为(
$actsAs=array('Containable');
)。然后像这样使用它:

['Entry'] => Array
    (
        [id] => 1
        [body] => 'xxxxxx'
        [...] => ...
    )

[Comment] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 1
                [body] => This is an example of a comment guys!
                [created] => 0000-00-00 00:00:00
            )

        [1] => Array
            (
                [id] => 2
                [user_id] => 1
                [body] => This is the 2nd comment!!!
                [created] => 0000-00-00 00:00:00
            )
    )
['Entry'] => Array
    (
        [id] => 1
        [body] => xxxxxx
        [...] => ...
    )

[Comment] => Array
    (
        [0] => Array
            (
              [Comment] => Array
                   (
                      [id] => 1
                      [user_id] => 1
                      [body] => This is an example of a comment guys!
                      [created] => 0000-00-00 00:00:00  
                   )

              [User] => Array
                   (
                      [id] => 1
                      [username] => myusername
                      [created] => 0000-00-00 00:00:00  
                   )
            )

        [1] => Array
            (
              [Comment] => Array
                   (
                      [id] => 1
                      [user_id] => 2
                      [body] => fasdfasfaT
                      [created] => 0000-00-00 00:00:00  
                   )

              [User] => Array
                   (
                      [id] => 2
                      [username] => myusername2
                      [created] => 0000-00-00 00:00:00  
                   )
            )
    )
$this->Entry->find('all', array(
    ...
    'contain' => array('Comment' => 'User')
));
['Entry'] => Array
(
    [id] => 1
    [body] => xxxxxx
    [...] => ...
)

[Comment] => Array
(
    [0] => Array
        (
          [id] => 1
          [user_id] => 1
          [body] => This is an example of a comment guys!
          [created] => 0000-00-00 00:00:00  
          [User] => Array
               (
                  [id] => 1
                  [username] => myusername
                  [created] => 0000-00-00 00:00:00  
               )
        )

)
结果如下所示:

['Entry'] => Array
    (
        [id] => 1
        [body] => 'xxxxxx'
        [...] => ...
    )

[Comment] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 1
                [body] => This is an example of a comment guys!
                [created] => 0000-00-00 00:00:00
            )

        [1] => Array
            (
                [id] => 2
                [user_id] => 1
                [body] => This is the 2nd comment!!!
                [created] => 0000-00-00 00:00:00
            )
    )
['Entry'] => Array
    (
        [id] => 1
        [body] => xxxxxx
        [...] => ...
    )

[Comment] => Array
    (
        [0] => Array
            (
              [Comment] => Array
                   (
                      [id] => 1
                      [user_id] => 1
                      [body] => This is an example of a comment guys!
                      [created] => 0000-00-00 00:00:00  
                   )

              [User] => Array
                   (
                      [id] => 1
                      [username] => myusername
                      [created] => 0000-00-00 00:00:00  
                   )
            )

        [1] => Array
            (
              [Comment] => Array
                   (
                      [id] => 1
                      [user_id] => 2
                      [body] => fasdfasfaT
                      [created] => 0000-00-00 00:00:00  
                   )

              [User] => Array
                   (
                      [id] => 2
                      [username] => myusername2
                      [created] => 0000-00-00 00:00:00  
                   )
            )
    )
$this->Entry->find('all', array(
    ...
    'contain' => array('Comment' => 'User')
));
['Entry'] => Array
(
    [id] => 1
    [body] => xxxxxx
    [...] => ...
)

[Comment] => Array
(
    [0] => Array
        (
          [id] => 1
          [user_id] => 1
          [body] => This is an example of a comment guys!
          [created] => 0000-00-00 00:00:00  
          [User] => Array
               (
                  [id] => 1
                  [username] => myusername
                  [created] => 0000-00-00 00:00:00  
               )
        )

)

根据我的经验,蛋糕查询深层关联不是很有效。在您的情况下,它将为每个
注释
生成一个查询,以获取其
用户
。我会让Cake只获取注释,然后提取注释的用户ID,在一个查询中获取所有具有这些ID的用户,然后将这些用户信息添加到原始结果中来避免这种情况。

CakePHP 2.2 for find方法中没有这样的参数“contain”:您需要启用behavior@Steve有@ori指出了最好的方法——什么是Containable?它将结果集“包含”到您定义的一组模型/表中。它在技术上做的是动态地重新定义模型绑定。这与您使用
$This->Model->bindModel()
$This->Model->unbindModel()
相同-它实际上使用这些。否则,将
$this->Model->recursive
设置为大于1的值(唯一的其他选项是2)是缓慢的。