簡體   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