HABTM related records not being filtered by conditions when using containable

62 views Asked by At

Model1 HABTM Model2. In Model1's model class, I have the following code:

public class Model1 extends AppModel
{
    function getResult()
    {
        $this->contain('Model2', array(
            'conditions' => array('Model2.name' => 'foo')
        ));
        $result = $this->findByRelatedId($careNoteId);
        return $result;
    }
}

The result has every related Model2 record. It should only return the Model2 record if that record's name is "foo". No error, the condition is just never added to the SQL.

Containable is declared in AppModel's actsAs property.

What gives?

1

There are 1 answers

0
mattalxndr On BEST ANSWER

My syntax was wrong. These syntaxes are correct:

        $this->contain(array(
            'Model2' => array(
                'conditions' => array('Model2.name' => 'foo')
            )
        ));

or

        $this->contain('Model2', array(
            'Model2' => array(
                'conditions' => array('Model2.name' => 'foo')
            )
        ));