1

記事のページにコメントを表示したいのですが、コメントはユーザーによって送信されます。現在、コメントは表示できますが、コメントに関連付けられているユーザーは表示できません。

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;

投稿コメント

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
        )
    )
)
4

2 に答える 2

1

試す

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

(PostComment と User の関係を設定していると仮定します)

于 2013-10-29T10:56:19.687 に答える
0

あなたは必要になるでしょう

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

また、User モデルと PostComment モデルの間に適切な関連付けを設定する必要があります。

于 2013-10-29T11:08:28.017 に答える