2013-10-29 50 views
1

我想在文章的頁面上顯示評論,我的評論是由用戶提交的。 目前我能夠顯示評論,但不能顯示與評論​​相關的用戶。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' => [email protected], 
       ... 
      ), 
      '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' => [email protected], 
       ... 
      ), 
      'title' => 'Super cool', 
      'content' => 'merci les loulous', 
      'created' => '2013-10-26 17:09:21', 
      'status' => false 
     ) 
    ) 
) 

回答

1

嘗試

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

(我假設你已經設置PostComment和用戶之間的關係),你需要

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

而且

0

您需要在用戶和PostComment模型之間建立適當的關聯。