cakephp: find statement with 'contain'

2019-07-18 08:03发布

问题:

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')
        )
    ));
}

回答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



回答2:

Here is cakephp documentation about contain



标签: cakephp find