繁体   English   中英

Cakephp在单个页面中显示帖子的所有评论数

[英]Cakephp display all number of comments of post in a single page

我想在一个页面中显示所有评论以及所有帖子。例如:我对POST A有3条评论,对于B具有2条评论,依此类推。

它应该看起来像这样:POST A-3条评论POST B-2条评论,依此类推

因此,我为此编写了代码:

  $result = $this->Comment->find('all',array('fields'=>array('Comment.post_id','Comment.comments'),'group'=>'Comment.post_id'));
  pr($a);exit;
  $this->set('a',$a);

我得到的数组是这个

           Array
 (
[0] => Array
    (
        [Comment] => Array
            (
                [post_id] => 1
                [comments] => Nice Post...Nice Post...Nice Post...
            )

    )

[1] => Array
    (
        [Comment] => Array
            (
                [post_id] => 3
                [comments] => wow..nice !!! Lorem Ipsum is simply dummy text of the printing and typesetting 
            )

    )

)

我对post_id = 1共3条评论
我希望所有三个评论放在一起。

find()删除,'group'=>'Comment.post_id'

完全取决于您的意思,有多种方法可以做到这一点。

在帖子中查找

问题描述是:

想要显示所有评论以及所有帖子

为此,您只需要在Post上执行查找,包括结果范围内的Comment。 如果您的关联定义正确,则意味着:

$result = $Post->find('all', array(
    'recursive' => 1
));

在这种情况下,$ result将采用以下形式:

[0] => Array
        (   
            [Post] => Array
                (   
                    [id] => 1
                    [title] => First article
                    [content] => aaa 
                    [created] => 2008-05-18 00:00:00
                )   
            [Comment] => Array
                (   
                    [0] => Array
                        (   
                            [id] => 1
                            [post_id] => 1
                            [author] => Daniel
                            [email] => dan@example.com
                            [website] => http://example.com
                            [comment] => First comment
                            [created] => 2008-05-18 00:00:00
                        )   
                    [1] => Array
                        (   
                            [id] => 2
                            [post_id] => 1
                            [author] => Sam 
                            [email] => sam@example.net
                            [website] => http://example.net
                            [comment] => Second comment
                            [created] => 2008-05-18 00:00:00
                        )   
                )
        )   
[1] => Array
        (   
            [Post] => Array
                (...

您可以使用可包含的行为来微调返回的信息量,而不必使用递归。

在帖子中查找-计算评论

要获取帖子列表及其评论数-您首先需要了解所需的查询类型-例如:

SELECT
    Post.id, 
    COUNT(Comment.id)
FROM
    posts as Post
LEFT JOIN
    comments as Comment ON (Post.id = Comment.post_id)
GROUP BY
    Post.id

没有加入,将不会列出任何没有评论的帖子。

有很多方法可以执行这种查询-这是一种:

$result = $Post->find('all', array(
    'fields' => array(
        'Post.id', 
        'COUNT(Comment.id)'
    ),
    'group' => array('Post.id'),
    'joins' => array(
        array(
            'table' => 'comments',
            'alias' => 'Comment',
            'conditions' => array(
                'Post.id = Comment.post_id'
            )
        )
    )
));

使用查找列表

除非使用聚合函数(计数,总和等),否则问题中的组查询的结构不正确。 要按帖子ID将所有评论分组-您可以使用查找列表

$result = $Comment->find('list', array(
    'fields' => array(
        'id',
        'comments',
        'post_id'
    )
));

以这种方式使用的响应格式将是:

$result['post_id']['id']['comment']

即:

array(
    1 => array(
        aa => 'Nice Post...Nice Post...Nice Post...',
        bb => 'Another comment for post id 1'
    ),
    3 => array(
        cc => 'wow..nice !!! Lorem Ipsum is simply dummy text of the printing and typesetting ',
        dd => 'Another comment for post id 3',
        ...
    )
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM