From The Blog

CakePHP joining tables

28 December 2012, in CakePHP

In many cases when we want to formulate complex queries, that need joins between tables, we can still add joins on the fly by adding it to the call of find

$students_of_grade = $this->User->find('list',array(

            'fields'=>array('UserData.user_id'),
'joins'=>array(
                     array(
                                 'table' => 'user_data',
                                 'alias' => 'UserData',
                                 'type' => 'inner',
                                 'foreignKey' => 'user_id',
                                 'fields'=>array('UserData.grade_id','UserData.user_id'),
                                 'conditions'=> array('UserData.user_id = User.id'),
                     )
             ),
'conditions'=>array('UserData.grade_id'=>$grade_id,'User.status'=>1)
));

 

it also works for paginated results



Post a Comment