emersonthis
emersonthis

Reputation: 33378

CakePHP: $this->Model->contain(...) changes search results for DIFFERENT model

We noticed that when we use find() after a contain() method from a different controller, it alters the results...

Inside ModelAController...

public function index() {

$this->ModelA->contain(...); //this affects the next find()

$this->loadModel('ModelB');
$var = $this->ModelB->find('all');

}

When the contain() method above is removed the find() works normally. Why?

Upvotes: 0

Views: 165

Answers (1)

Andrea Alhena
Andrea Alhena

Reputation: 1046

As written in the CakePHP Documentation:

Containable allows you to streamline and simplify operations on your model bindings. It works by temporarily or permanently altering the associations of your models. It does this by using supplied the containments to generate a series of bindModel and unbindModel calls.

The unbind / bind Model functions work (if not specified with the right parameter) only for the first "find" call. Maybe the "contain" call doesnt release at all the effect of the internal unbind / bind calls performed.

What would I do in your case? Try performing your find using something like this:

$this->Model->find('all', array('contain' => array(/* YOUR DIRECTIVES */), 'conditions' => array('/* YOUR CONDITIONS */));

Maybe this will release the effect of the unbind / bind calls. Give it a try!

Obviously, remember to attach the "Containable" Behaviour to your model ;)

Upvotes: 1

Related Questions