Rob
Rob

Reputation: 21

Containable "contain" property not being reset

Containable is not behaving as I expect in CakePHP 2.1.2 in the following code:

class ReportCard extends AppModel {

....
    // debug shows expected results
    public function test1(){
        $this->Behaviors->attach('Containable');
        $params = array(
                'conditions' => array('ReportCard.id' => 1),
                'contain' => array(
                        'ReportCardGradingPeriodCollection' => array(
                                'GradingPeriodCollection')));

        debug($this->find('first', $params));
        $this->Behaviors->detach('Containable');
    }

    // Unexpected: debug shows same array as test1
    public function test2(){
        $this->Behaviors->attach('Containable');
        $params = array(
                'conditions' => array('ReportCard.id' => 1),
                'contain' => array(
                        'ReportCardGradingPeriodCollection' => array(
                                'GradingPeriodCollection' => array(
                                        'GradingPeriodCollectionDetail' => array(
                                                'GradingPeriod')))));

        debug($this->find('first', $params));
        $this->Behaviors->detach('Containable');
    }

}

I get unexpected results when calling the functions from the controller. test1() shows the expected array. test2() shows the same array from test1. If I run test2() then test1() I get expected results (large array, then small array).

class ReportCardsController extends AppController {

    ....

    public function test(){
        $this->ReportCard->test1();
        $this->ReportCard->test2();
    }

}

I have tried using actAs in the model instead of dynamically loading the behavior in each function, but that did not help.

I apologize if I'm missing something simple. Thanks in advance!

Upvotes: 0

Views: 147

Answers (1)

ADmad
ADmad

Reputation: 8100

Quoting the manual:

Containable must to be attached to all models used in containment,

From your example code it's not clear whether you are doing that.

And here's the remaining half of that note:

you may consider attaching it to your AppModel.

Upvotes: 1

Related Questions