简体   繁体   English

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

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

I want to display all comments along with all posts in a single page.For eg : I have 3 comments for POST A,2 comments for post B and so on. 我想在一个页面中显示所有评论以及所有帖子。例如:我对POST A有3条评论,对于B具有2条评论,依此类推。

It should look like this : POST A - 3 Comments POST B - 2 Comments and so on 它应该看起来像这样:POST A-3条评论POST B-2条评论,依此类推

So for this I have coded this : 因此,我为此编写了代码:

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

The array I got is this 我得到的数组是这个

           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 
            )

    )

)

I have total 3 comments for post_id = 1 我对post_id = 1共3条评论
I want all the three comments come together. 我希望所有三个评论放在一起。

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

Depending on exactly what you mean, there are a number of ways to do that. 完全取决于您的意思,有多种方法可以做到这一点。

Find on Post 在帖子中查找

The question description is: 问题描述是:

want to display all comments along with all posts 想要显示所有评论以及所有帖子

For that you'd only need to perform a find on Post, including Comment in the scope of the results. 为此,您只需要在Post上执行查找,包括结果范围内的Comment。 If your associations have been defined correctly that means: 如果您的关联定义正确,则意味着:

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

In this case, $result will be of the form: 在这种情况下,$ 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
                (...

Instead of using recursive, you can use the containable behavior to fine-tune the amount of information returned. 您可以使用可包含的行为来微调返回的信息量,而不必使用递归。

Find on Post - counting comments 在帖子中查找-计算评论

To get a list of posts and their comment counts - you need first to understand the sort of query required - eg: 要获取帖子列表及其评论数-您首先需要了解所需的查询类型-例如:

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

Without the join, any post with no comments will not be listed. 没有加入,将不会列出任何没有评论的帖子。

There are many ways to execute a query of that kind - here's one: 有很多方法可以执行这种查询-这是一种:

$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'
            )
        )
    )
));

Use find list 使用查找列表

The group query in the question isn't the right structure unless there's an aggregate function used (count, sum, etc.). 除非使用聚合函数(计数,总和等),否则问题中的组查询的结构不正确。 To get all comments grouped by post id - you can however use find list : 要按帖子ID将所有评论分组-您可以使用查找列表

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

Used in this way the response format will be: 以这种方式使用的响应格式将是:

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

ie: 即:

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