Get post with comments

2013-01-09 Thread Max Power
Hi,

I want to paginate over posts which has some comments. That is, I want to 
get from the database only those posts which has al least one comments and, 
for each post, all the associated comments:

$post = array(
[0] = array(
Post,
Comments = array(
[0] = comment,

 )
   )
)

How can I do that?

I have got posts with some comments joining posts and commets tables, but I 
don't know how to get all comments for each post.

$this-paginate['joins'] = array(
'table' = 'comments',
'alias' = 'Comment',
'type' = 'RIGHT',
'conditions' = array('Comment.post_id  = Post.id');
);
$this-paginate['group'] = array('Post.id');
$posts = $personas = $this-paginate(null);

Kind regards,

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.




Re: Get post with comments

2013-01-09 Thread lowpass
On Wed, Jan 9, 2013 at 5:34 AM, Max Power laran.ta...@gmail.com wrote:
 Hi,

 I want to paginate over posts which has some comments. That is, I want to
 get from the database only those posts which has al least one comments and,
 for each post, all the associated comments:

The simplest way to handle this is to use counterCache. Add a column,
comment_count, to your posts table. In the Comment model ...

public $belongsTo = array(
'Post' = array(
'className' = 'Post',
'foreignKey' = 'post_id',
'counterCache' = 'comment_count'
)
//...
);

Whenever a new Comment is added the comment_count value will be incremented.

public $paginate = array(
'fields' = ...,
'limit' = ...,
'order'= ...,
'conditions' = array(
'Post.comment_count ' = 0
),
'contain' = array('Comment')
);

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.