cakephp 2.0 hasmany relation检索数据

时间:2012-09-27 10:23:28

标签: count cakephp-2.0 has-many

我有两个型号名称图片和评论。这里的关系就像图像有很多评论。现在在我的列表页面中,我想要显示所有的imae细节,并且只显示该图像的评论。你能告诉我怎么办? 在我写查询后,我的返回数据是

Array
(

    [0] => Array
        (
            [Image] => Array
                (
                    [image_id] => 57
                    [user_id] => 1
                    [category_id] => 22
                    [image_title] => scroul
                    [description] => beutifull natural image for the animal
                    [keyword] => scrual
                    [image_price] => 
                    [image_name] => 7bf4a72509da5906903c84e88228b9dd.jpg
                    [image_path] => img/uploads/images/original/
                    [image_available_size] => 
                    [like] => 12
                    [size] => 3244
                    [resolution] => 2162 x 1644
                    [i_date] => 1348573022
                    [i_by] => 1
                    [u_date] => 1348573022
                    [u_by] => 1
                    [is_active] => Y
                    [is_deleted] => N
                )

            [Comment] => Array
                (
                    [0] => Array
                        (
                            [comment_id] => 5
                            [image_id] => 57
                            [user_id] => 2
                            [comment] => socute
                            [comment_date] => 1348739230
                            [is_active] => N
                            [is_deleted] => N
                        )

                )

        )

    [1] => Array
        (
            [Image] => Array
                (
                    [image_id] => 56
                    [user_id] => 1
                    [category_id] => 22
                    [image_title] => cute dog
                    [description] => cute dog looking 
                    [keyword] => 
                    [image_price] => 
                    [image_name] => d4af899b0d52cccbec94952a3abd0077.jpg
                    [image_path] => img/uploads/images/original/
                    [image_available_size] => 
                    [like] => 8
                    [size] => 620
                    [resolution] => 2592 x 1944
                    [i_date] => 1348572897
                    [i_by] => 1
                    [u_date] => 1348572897
                    [u_by] => 1
                    [is_active] => Y
                    [is_deleted] => N
                )

            [Comment] => Array
                (
                    [0] => Array
                        (
                            [comment_id] => 3
                            [image_id] => 56
                            [user_id] => 2
                            [comment] => ohhhhhhh
                            [comment_date] => 1348737968
                            [is_active] => N
                            [is_deleted] => N
                        )

                )

        )

    [3] => Array
        (
            [Image] => Array
                (
                    [image_id] => 55
                    [user_id] => 1
                    [category_id] => 22
                    [image_title] => ships
                    [description] => ships with beutiful green background
                    [keyword] => ship,green,animal,nature,background,eating,white ship
                    [image_price] => 
                    [image_name] => c0dfc2432ae047e9160f3ef99880fe87.jpg
                    [image_path] => img/uploads/images/original/
                    [image_available_size] => 
                    [like] => 1
                    [size] => 1831
                    [resolution] => 2520 x 1944
                    [i_date] => 1348572846
                    [i_by] => 1
                    [u_date] => 1348661976
                    [u_by] => 1
                    [is_active] => Y
                    [is_deleted] => N
                )

            [Comment] => Array
                (
                    [0] => Array
                        (
                            [comment_id] => 2
                            [image_id] => 55
                            [user_id] => 2
                            [comment] =>  i like it
                            [comment_date] => 1348737942
                            [is_active] => Y
                            [is_deleted] => N
                        )

                    [1] => Array
                        (
                            [comment_id] => 4
                            [image_id] => 55
                            [user_id] => 2
                            [comment] =>  good scene
                            [comment_date] => 1348738004
                            [is_active] => N
                            [is_deleted] => N
                        )
                )
        )
)

在上面的数组中有该图像的所有注释。我不想在这里评论列表我只是不想发表评论。

2 个答案:

答案 0 :(得分:1)

您可以发布您正在使用的查找语句吗?您可以从文档中将递归设置为特定级别:

  
      
  • -1 Cake仅提取组数据,无连接。
  •   
  • 0 Cake获取组数据及其域
  •   
  • 1 Cake获取组,其域及其关联用户
  •   
  • 2 Cake获取组,其域,关联用户和用户关联文章
  •   

因此,如果您只想要当前模型中的数据,则可以调用以下内容:

$this->Model->find('all', array('recursive' => -1));

还有Containable行为,允许您在调用find时指定要检索的模型数据。

假设您有一个hasMany图片的帖子模型。正确包含Post Model中的Containable行为的调用将是:

$this->Post->find('all', array(
 'conditions' => array('Post.id' => 1),
 'contain' => array('Image')
));

修改 由于拼写和格式化,我误读了你的初始问题。我以为你想要“no comments”出现在数据数组中,而你只想要出现“注释数量

如果您想要评论计数,请使用Kishor Kundan建议的counterCache

答案 1 :(得分:1)

Cake提供了另一种神奇的魔力,“counterCache”。

您可以在评论模型中定义counterCache

public $belongsTo = array(
    'className' => 'Image',
    'foreignKey' => <your_foreign_key>,
    ...
    ...
    'counterCache' => true

);

然后在图像表中添加一个字段'comment_count'(或模型图像正在使用的表格),蛋糕将为您完成其余的工作。

每次添加/删除评论时,这确实会增加开销,但除了每次获取图像数据时发出“计数”时,这都是更好的选择。

有关详情,请查看the cookbook。在那里留意“counterCache”。

<强>更新

要限制计数器缓存的范围,请使用附加属性'counterScope'作为

public $belongsTo = array(
    'className' => 'Image',
    'foreignKey' => <your_foreign_key>,
    ...
    ...
    'counterCache' => true,
    'counterScope' => array('Image.active' => 1)

);