CakePHP显示用户的评论

时间:2013-10-29 10:49:20

标签: cakephp

我想在文章的页面上显示评论,我的评论是由用户提交的。 目前,我可以显示评论,但不能显示与评论​​相关的用户。

SQL: 讯息

CREATE TABLE `posts` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `slug` varchar(255) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `content` longtext CHARACTER SET utf8 NOT NULL,
  `created` datetime NOT NULL,
  `active` tinyint(1) DEFAULT '0',
  `picture` tinyint(1) DEFAULT NULL,
  `type` enum('Article','News','Page') CHARACTER SET utf8 DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

post_comments

CREATE TABLE `post_comments` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `post_id` int(11) DEFAULT NULL,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) NOT NULL DEFAULT '',
  `content` text NOT NULL,
  `created` datetime NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-

$post = $this->Post->find('first', array(
            'conditions' => array('Post.slug' => $slug, 'Post.active' => '1'),
            'contain' => array('PostComment')
        ));

-

debug($post);

array(
    'Post' => array(
        'id' => '2',
        'name' => 'azeaeaez',
        'slug' => 'azeaeaez1',
        'content' => 'aeaeaeaze',
        'created' => '2013-10-21 12:33:30',
        'active' => true,
        'picture' => null,
        'type' => 'News',
        'user_id' => null
    ),
    'PostComment' => array(
        (int) 0 => array(
            'id' => '4',
            'post_id' => '2',
            'user_id' => '6',
            'title' => 'Super comme article',
            'content' => 'Merci c'est cool j'en prend note !',
            'created' => '2013-10-29 08:58:07',
            'status' => false
        ),
        (int) 1 => array(
            'id' => '3',
            'post_id' => '2',
            'user_id' => '31',
            'title' => 'Super cool',
            'content' => 'merci les loulous',
            'created' => '2013-10-26 17:09:21',
            'status' => false
        )
    )
)

期望的结果

array(
    'Post' => array(
        'id' => '2',
        'name' => 'azeaeaez',
        'slug' => 'azeaeaez1',
        'content' => 'aeaeaeaze',
        'created' => '2013-10-21 12:33:30',
        'active' => true,
        'picture' => null,
        'type' => 'News',
        'user_id' => null
    ),
    'PostComment' => array(
        (int) 0 => array(
            'id' => '4',
            'post_id' => '2',
            'user' => array(
                'user_name' => 'toto',
                'email' => tot@email.com,
                ...
            ),
            'title' => 'Super comme article',
            'content' => 'Merci c'est cool j'en prend note !',
            'created' => '2013-10-29 08:58:07',
            'status' => false
        ),
        (int) 1 => array(
            'id' => '3',
            'post_id' => '2',
            'user' => array(
                'user_name' => 'toto',
                'email' => tot@email.com,
                ...
            ),
            'title' => 'Super cool',
            'content' => 'merci les loulous',
            'created' => '2013-10-26 17:09:21',
            'status' => false
        )
    )
)

2 个答案:

答案 0 :(得分:1)

'contain' => array('PostComment', 'PostComment.User')

(我假设你已经设置了PostComment和User之间的关系)

答案 1 :(得分:0)

你需要

$post = $this->Post->find('first', array(
  'conditions' => array('Post.slug' => $slug, 'Post.active' => '1'),
  'contain' => array('PostComment' => 'User')
));

您需要在User和PostComment模型之间建立正确的关联。