Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP Contains not working with Recursive

When I try:

// Removed the limit to ensure that all of the group notes items can be found and collapsed
$recent_notes = $this->User->Note->find('all', array(
    'recursive' => 2,
    'order' => 'Note.created DESC',
    'conditions' => $conditions,
    'contains' => array(
        'NotesUser', 'Poster', 'Comment' => array('Poster')
    )
));

It does not limit the output whatsoever - I get every related model. However, when I don't specify recursive as 2, or if I specify it as 1, I am missing the Comment=>Poster model.

How can I get only the models I need? Thanks!

like image 512
Garrett Avatar asked Jan 21 '26 14:01

Garrett


1 Answers

To get only the models you need, use the [Containable behavior]:

  • set recursive to -1
  • use 'contain' singular, NOT 'contains' plural, like you have
  • make sure you're setting the $actsAs variable in your model: public $actsAs = array('Containable');

Everyone that I know sets $this->recursive = -1; in the AppModel... this defaults everything to recursive -1 so you don't ever have to set it again unless you want to include more data...and in those cases, I almost always use contain(), not $recursive.

like image 79
Dave Avatar answered Jan 23 '26 07:01

Dave