0

我有两个型号名称图像和评论。这里的关系就像图像有很多评论。现在在我的列表页面中,我想显示所有 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
                        )
                )
        )
)

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

4

2 回答 2

1

您能否发布您正在使用的查找语句。您可以从文档中将递归设置为某个级别:

  • -1 Cake 仅获取组数据,不获取连接。
  • 0 Cake 获取组数据及其域
  • 1 Cake 获取一个组、它的域及其关联的用户
  • 2 Cake 获取一个组、它的域、它的关联用户以及用户的关联文章

因此,如果您只需要来自当前模型的数据,则可以调用以下命令:

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

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

假设您有一个 Post 模型,其中hasManyImage. 正确包含 Post 模型中的 Containable 行为的调用将是:

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

编辑: 由于拼写和格式,我误读了您最初的问题。我以为您希望“无评论”出现在数据数组中,而不是只希望出现“评论数”

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

于 2012-09-27T15:38:31.677 回答
1

Cake 提供了另一个神奇的魔法,“counterCache”。

您可以在 Comments 模型中定义 counterCache

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

);

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

每次添加/删除评论时,这确实会增加开销,但它比每次获取图像数据时发出“计数”要好得多。

有关更多信息,您可以查看食谱。在那里寻找“counterCache”。

更新:

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

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

);
于 2012-09-28T18:38:59.683 回答