简体   繁体   English

CakePHP:列出与帖子相关的评论

[英]CakePHP: List comments associated with a post

In my application I have two tables (I have more but I'm only discussing two here): 在我的应用程序中,我有两个表(我有更多表,但这里只讨论两个表):

**Posts**
id
title
datetime
content

**Comments**
id
content
post_id
user_id

As you can see their is a relationship using the post_id as a foreign key. 如您所见,它们是使用post_id作为外键的关系。

When viewing the Post I have the following code to display comments: 查看帖子时,我有以下代码来显示评论:

<?php if ( ! empty($post['Comment']) ): ?>
        <ul>
            <?php foreach ($post['Comment'] as $comment): ?>
            <li>
                <?php echo $comment['title']; ?>
            </li>
            <?php endforeach; ?>
        </ul>
        <?php else: ?>
        <p>No comments...</p>
        <?php endif; ?>

Here is the controller method for viewing a post: 这是用于查看帖子的控制器方法:

function view ( $id = null, $slug = null )
    {
        $post = $this->Post->find('first',array('contain'=>array('User'=>array('Comment','Profile')),'conditions'=>array('Post.id'=>Tiny::reverseTiny($id))));

        if (!$post)
        {
            throw new NotFoundException('404');
        }
        else if($post['Post']['status'] == '0') // 0=draft 1=open 2=open
        {
            if($post['Post']['user_id'] == $this->Auth->user('id'))
            {
                $this->Session->setFlash('Your post has NOT been published yet');
            }
            else
            {
                throw new NotFoundException('404');
            }
        }

        if (Inflector::slug($post['Post']['title']) != $slug || $slug = null)
        {
            $this->redirect(array('id'=>Tiny::toTiny($post['Post']['id']),'slug'=>Inflector::slug($post['Post']['title'])));
        }

        $this->set(compact('post'));
    }

and this is the comment model: 这是评论模型:

class Comment extends AppModel
{
    public $name = 'Comment';

    public $belongsTo = array('User','Post');

    public $actsAs = array('Containable');
}

and a typical url for a post would be: /posts/Sample_post-1ns 而帖子的典型网址为: /posts/Sample_post-1ns

However I ALWAYS get the message that there are no comments... But I have looked in the database and their is... and I have double checked that all the associations are correct. 但是,我总是收到没有评论的消息。。。但是我已经查看了数据库,发现它们是...,并且我再次检查了所有关联是否正确。 So I can only presume that the code above is wrong! 所以我只能假定上面的代码是错误的!

Also is their a better way of calling the comments? 他们打电话说的更好方法吗? As I want to add the ability to filter and paginate them too. 因为我也想添加过滤和分页功能。

Cheers 干杯

Seems you need something like this: 似乎您需要这样的东西:

Comment model: 评论模型:

public $belongsTo = array('User', 'Post');

Post model: 帖子模型:

public $belongsTo = array('User');
public $hasMany = array('Comment');

User model: 用户模型:

public $hasMany = array('Post', 'Comment');

And contain statement of Post: contain Post声明:

'contain' => array('Comment', 'User' => array('Comment', 'Profile'))

From your schema it looks like the comment model is related to the post model, not to the user, so: 从您的模式看来,注释模型与发布模型有关,而不与用户有关,因此:

$post = $this->Post->find('first',array('contain'=>array('User'=>array('Comment','Profile')),'conditions'=>array('Post.id'=>Tiny::reverseTiny($id))));

Should be: 应该:

$post = $this->Post->find('first',array('contain'=>array('Comment','User'=>array('Profile')),'conditions'=>array('Post.id'=>Tiny::reverseTiny($id))));

Ie the Comment should be outside of the User contain array. 即,Comment应该在User contains数组之外。

Providing that the correct hasMany and belongsTo relationships are set up on the Post/User and Comment model respectively, the first example would produce an array like this: belongsTo分别在Post / User和Comment模型上设置了正确的hasManybelongsTo关系,则第一个示例将生成如下数组:

 array(
    'Post'=>array(...),
    'User'=>array(
        'id'=>1,
         //Other user data

         'Comment'=>array(
              array(...), //Comment 1
              array(...), //Comment 2
          )
     )
 );

And the second example would produce: 第二个示例将产生:

array(
    'Post'=>array(...),
    'User'=>array('id'=>1 //Other user data)
    'Comment'=>array(
          array(...), //Comment 1
          array(...), //Comment 2
     )
 );

Hopefully, that shows that the difference is in the position of the "Comment" key in the array - it is either under the User key, or at the top level depending on how you do the contain. 希望这表明差异在于数组中“ Comment”键的位置-它位于User键之下,还是位于顶层,具体取决于您如何进行包含。

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

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