3

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!

Garrett
  • 11,451
  • 19
  • 85
  • 126

3 Answers3

8

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.

Dave
  • 28,833
  • 23
  • 113
  • 183
  • Thanks again, Dave. Unfortunately for you, swiecki beat you to the answer, but know that if I could mark both of you as correct I would :) – Garrett Feb 29 '12 at 19:07
  • We both kind of posted it exactly the same time :) It's happened before and it'll happen again, no worries. – swiecki Feb 29 '12 at 19:08
  • 2
    Yah - always happens that way. Garrett: Suggestion moving forward - mark the best answer, not the first answer. It will benefit the StackOverflow community more than just marking whoever's first. I'm not necessarily saying mine is - just a suggestion for future. We're not in it for competition / to get rep - we're in it to help people! :) – Dave Feb 29 '12 at 19:11
  • 1
    Thanks for the advice. In this case it was just a simple syntax error that you both caught instantly, but I have and will continue to look for the best answer moving forward. – Garrett Feb 29 '12 at 19:36
  • @Garrett This is 4 years ago, but I can't help but notice that Dave "answered Feb 29 '12 at 19:01" and sweicki "answered Feb 29 '12 at 19:03"... so Dave beat him by 2 minutes. – csga5000 Jul 27 '16 at 20:57
2

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.

adriaroca
  • 172
  • 11
swiecki
  • 3,483
  • 3
  • 23
  • 19
1

If you have deep association, for example History belongsTo Warehouse and Warehouse belongsTo Company and you want only Warehouse.Company, you can also use

'contain'   => array('Warehouse.Company')
gdm
  • 7,647
  • 3
  • 41
  • 71