cakephp: find statement with 'contain'

14,739

Solution 1

By default when a find statement executes cake pulls all the data from the model on which the find function is executing plus all the data from the models that are associated with the model. Most of the time you don't need that extra data, Cake has containable behaviour for exactly that purpose. You can specify which associated model's data you want in your result.

In the above example find statement will fetch the first record from the User model plus associated data from Access and Moderator models.

Here is the link from cakephp book http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

Solution 2

Here is cakephp documentation about contain

Share:
14,739
vaanipala
Author by

vaanipala

Updated on June 04, 2022

Comments

  • vaanipala
    vaanipala almost 2 years

    the following User model function is from MilesJones forum plugin. Can someone tell me on what is the use of 'contain' in the find stmt. I couldn't find any example with contain in the cakephp cookbook. Any helps is appreciated.

    public function getProfile($id) {
        return $this->find('first', array(
            'conditions' => array('User.id' => $id),    
            'contain' => array(
                'Access' => array('AccessLevel'),
                'Moderator' => array('ForumCategory')
            )
        ));
    }