Reputation: 5962
So, I have a system with users, users have posts and posts have comments. Everything is well linked with the models.
What I'ld like to do is getting all the last comments from all the posts of one specific user.
And I'm a bit stuck. Any ideas ?
Upvotes: 1
Views: 86
Reputation: 7525
Do something like this from UsersController
(not tested):
$comments = $this->User->Post->Comment->find('all', array(
'recursive' = 1,
'limit' => 4,
'order' => array('Comment.created' => 'DESC'),
'conditions' => array('Post.user_id' => $id_user)
));
You can change recursive
to 2
if you need to retrieve User
as well.
Upvotes: 1
Reputation: 5303
You can try this:
$this->User->Behaviors->load('Containable');
$this->User->contain(array(
'Post' => array(
'order' => array('Post.created' => 'DESC'),
'Comment' => array(
'limit' => 4,
'order' => array('Comment.created' => 'DESC')
)
)
));
$rs = $this->User->read(null, $id_user);
$this->User->Behaviors->unload('Containable');
Your result is on $rs variable.
Reference:
Hope this helps.
Upvotes: 2