Containable with mixed arguments, the CakePHP Beauty!

Here’s an example of CakePHP Containable behaviour. Although, it was not a very complex find call but the beauty of the containable is this that you can fetch records using “tricky ways” which normally may not be possible with straight associations find calls.

For example look at the following code at the line 6. I could call the association of model Control which is User model and I could have model name and mixed fields in the same array of argument. Then I could supply the field names of the User model in the same array along with id and control_name which are related to Control model.

[php collapse=”false”]
$this->Control->ControlsAssignee->Behaviors->load(‘Containable’);
$options = array(
‘conditions’ => array(‘ControlsAssignee.’ . $this->Control->primaryKey => $id),
‘contain’=>array(
‘Assignee’=>array(‘full_name’),
‘Control’=>array(‘User’, ‘User.id’, ‘User.full_name’, ‘id’, ‘control_name’),
‘ControlCompletion’=>array(‘Upload’)
)
);[/php]

Leave a Reply