使用关系在CakePHP中显示数组的第4级

时间:2011-06-01 10:38:29

标签: cakephp cakephp-1.3

我有表项目,截图,sub_screenshot和用户
 我想实现这种图表。它就像获得第四级。

[Model]     
     Array
                (
                    [0] => Array
                        (
                            [Project] => Array
                                (
                                    [id] => 2
                                )
                            [Screenshot] = Array
                                (
                                    [0] => Array
                                       (
                                             [Screenshot] => Array 
                                                  (
                                                     [project_id] => 2
                                                     [id] => 1
                                                  )
                                             [SubScreenshot] = Array
                                                  (
                                                         [0] => Array 
                                                               (
                                                                  [id] = 1
                                                                  [project_id] = 2
                                                                  [screenshot_id] = 1
                                                               )
                                                         [1] => Array 
                                                               (
                                                                  [id] = 2
                                                                  [project_id] = 2
                                                                  [screenshot_id] = 1
                                                               )
                                                  )

                                       )
                                    [1] => Array
                                       (
                                             [Screenshot] => Array 
                                                  (
                                                     [project_id] => 3
                                                     [id] => 1
                                                  )
                                             [SubScreenshot] = Array
                                                  (
                                                         [0] => Array 
                                                               (
                                                                  [id] = 1
                                                                  [project_id] = 3
                                                                  [screenshot_id] = 1
                                                               )
                                                         [1] => Array 
                                                               (
                                                                  [id] = 2
                                                                  [project_id] = 3
                                                                  [screenshot_id] = 1
                                                               )
                                                         [2] => Array 
                                                              (
                                                                 [id] = 2
                                                                 [project_id] = 3
                                                                 [screenshot_id] = 1
                                                              )
                                                  )

                                       )
                              )
                          [User] => Array 
                             (
                                  [id] => 1
                             )
                      )
                )

此致

2 个答案:

答案 0 :(得分:0)

为了让您了解我建议您使用afterFind()回调

http://book.cakephp.org/view/1050/afterFind

在find之后,将User设置为使用User模型进行另一次查找。

我不知道是否存在任何配置来修改$recursive而没有修改核心。

修改

您可以在AppModel中创建一个通用的afterFind,以检查是否有$ recursive = 4并复制关系的核心功能并生成它们。

答案 1 :(得分:0)

使用可包含的行为。我最近也有一些问题。将递归设置为4会更有效,因为您只需要包含所需的模型。

按照以下说明设置模型的行为。 http://book.cakephp.org/view/1323/Containable

然后,您应该能够执行查找并获得所需的结构。

$this->Project->find(
    'all',
    array(
        'conditions' => array(
            //put your conditions in here
        ),
        'contain' => array(
             'User',
             'Screenshot',
             'Screenshot.SubScreenshot'
             'Scrrenshot.SubScreenshot.User'
    )
);

有几种方法可以使用模型语法。您可以将数组添加到包含的模型中以添加条件和/或进一步深度。