2013-10-29 5 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') 

당신은

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

그리고이 필요합니다

0

(난 당신이 PostComment와 사용자 사이의 관계를 설정했다고 가정) 시도 원하는 결과 User와 PostComment 모델 사이에 적절한 연결을 설정해야합니다.

관련 문제