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

时间:2012-03-01 15:01:36

标签: php cakephp

在我的应用程序中,我有两个表(我有更多,但我只在这里讨论两个):

**Posts**
id
title
datetime
content

**Comments**
id
content
post_id
user_id

正如您所看到的,他们是使用post_id作为外键的关系。

查看帖子时,我有以下代码来显示评论:

<?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; ?>

以下是查看帖子的控制器方法:

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

这是评论模型:

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

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

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

并且帖子的典型网址为:/posts/Sample_post-1ns

然而,我总是得到没有评论的消息......但我已经查看了数据库,他们是......我已经仔细检查了所有关联是否正确。所以我只能假设上面的代码是错误的!

他们是一个更好的方式来发表评论吗?因为我想添加过滤和分页它们的能力。

干杯

2 个答案:

答案 0 :(得分:3)

似乎你需要这样的东西:

评论模型:

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

发布模型:

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

用户模型:

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

contain发表声明:

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

答案 1 :(得分:1)

从您的架构看,评论模型看起来与帖子模型有关,而不是与用户有关,因此:

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

应该是:

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

即。注释应该在User包含数组之外。

假设分别在Post / User和Comment模型上设置了正确的hasManybelongsTo关系,第一个示例将产生如下数组:

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

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

第二个例子会产生:

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

希望这表明区别在于数组中“注释”键的位置 - 它位于用户键下,或者位于顶层,具体取决于您如何进行包含。