Wednesday, January 9, 2013

Re: Get post with comments

On Wed, Jan 9, 2013 at 5:34 AM, Max Power <laran.tages@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+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate