In addition to Bill answer , to get countas a field User, you need to get it asvirtualField
$this->User->virtualFields['count'] = 0;
$this->User->find('all', [
'fields' => array(
'User.id',
'COUNT(User.id) as User__count'
),
'group' => 'User.id'
]);
This will be your result:
array(
(int) 0 => array(
'User' => array(
'id' => '1',
'count' => '2'
)
),
(int) 1 => array(
'User' => array(
'id' => '2',
'count' => '1'
)
)
)
source
share