is it good or bad practice in CakePHP to have conditions set in the contain
of a find query like:
$data = $this->SomeModel->find('all', array(
'contain' => array(
'AnotherModel' => array(
'conditions' => array(
// some conditions
)
)
)
));
In which cases will putting conditions inside a contain be useful, and when should I use it or not. Sorry, this is still confusing to me.
Thank you
I'm not sure whether this is a good idea... First of all, I'm going to assume that this specific case requires you to specify conditions that unique to this case, i.e. not general enough for you to put into your
SomeModel
model relationship criteria toAnotherModel
.My suggestions would be that you should put those conditions in your overall find conditions, as contain specifies which linked models to return (controlling the joins under the hood). Like in SQL, you can join another table and specify which records to match in your
WHERE
clause.From the manual, you can specify conditions in your contain, but they won't affect the results that don't join your model like the overall conditions will.
I'd do this: