Question

I have a model Post that have a lot of another models relationships.. in some part of the app I want select one post and just bring Comment model. I'm doing this:

$this->Post->find('fist', array('contain'=> 'Comment'));

Its working because is returning just Comment model and not a lot of another related models that i dont want.. the issue is. I want choose the models the Comment model will return, in this case I want the model User that is related to Comment model. I want my array looks like that:

'Post'
  title=> 'Title here'
'Comment'
  text=> 'Comment here.. its good etc'
  'User'
    name=> 'Jason Miller'
Was it helpful?

Solution

The containable behavior can do deep associations. Assuming your Comment model is associated with User already, just do the following:

$this->Post->find('first', array('contain' => array('Comment' => array('User'))));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top