manohar khadka manohar khadka - 6 months ago 103
PHP Question

Group By within contain cakephp

Hellow , I want to use group by within contain in cakephp. In the following case i want to take only distinct organization within organizationUser array..

$options = array(
'conditions' => array('User.' .$this->User->primaryKey => $userId),
'contain' => array(
'OrganizationUser'=>array(
'conditions'=>['status'=>3],
'group'=> array( 'OrganizationUser.organization_id')),
'OrganizationUser.Organization',
'OrganizationUser.Organization.Noticeboard',
'OrganizationUser.Organization.Newsboard',
'OrganizationUser.Organization.Noticeboard.Branch',
),
'page'=>$page,
'limit'=>$limit
);



$org = $this->User->find('all', $options);


But this is throwing error like 'Column not found', and 'conditions' is working fine within OrganizationUser but 'group' not working.I am using cakephp version 2.Thanks in advance.

Answer

I don't think cakephp 2+ offer something like you are doing to make field distinct within contain. So better to try following.. Replace :

'group'=> array( 'OrganizationUser.organization_id')

By

'fields'=> array( 'DISTINCT OrganizationUser.organization_id')

that might work for you.

Comments