manohar khadka manohar khadka - 1 year ago 215
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(
'group'=> array( 'OrganizationUser.organization_id')),

$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 Source

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


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

that might work for you.