Search code examples
cakephpmodelcakephp-1.2containable

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!


Solution

  • Recursive is ignored as soon as you set contain.

    From the docs:

    The ContainableBehavior has a number of options that can be set when the Behavior is attached to a model. The settings allow you to fine tune the behavior of Containable and work with other behaviors more easily.

    recursive (boolean, optional) set to true to allow containable to automatically determine the recursiveness level needed to fetch specified models, and set the model recursiveness to this level. setting it to false disables this feature. The default value is true.

    Make sure the array key is set as 'contain', not 'contains' as you've posted above, so like:

    $this->Post->find('all', array('contain' => 'Tag'));
    

    I'm also assuming that you're loading and attaching the containable behavior correctly. If you're having trouble, the documentation is here.