eclaude
eclaude

Reputation: 886

CakePHP show post comments with user

I want to display the commentairs on the page of the article, my comments are submitted by users. Currently I am able to display the comments but not the users associated with comments.

SQL: posts

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

the desired result

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

Upvotes: 1

Views: 385

Answers (2)

Mark
Mark

Reputation: 3389

You'll need

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

And you need to set up the proper association between the User and PostComment models.

Upvotes: 0

arilia
arilia

Reputation: 9398

try

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

(I assume that you have set the relationship between PostComment and User)

Upvotes: 1

Related Questions