I'd like to know what's wrong with my query. I'm debugging it, and it's bringing all the results of the ModelHorario template and not just one result per user, ie the fields, group, and order are not being considered.
$us['joins'] = array(
array(
'table' => 'registro_horarios',
'alias' => 'RegistroHorario',
'type' => 'LEFT',
'fields' => array(
'(SELECT cliente_id
FROM registro_horarios m
WHERE m.user_id = registro_horarios.user_id
ORDER BY data_fim DESC LIMIT 1) as cliente_id',
'RegistroHorario.user_id'
),
'conditions' => array(
'User.id = RegistroHorario.id'
),
'group' => 'RegistroHorario.user_id',
'order' => 'RegistroHorario.MAX(data_fim) DESC'
));
$us['fields'] = array(
'User.id'
);
$this->loadModel('User');
$usuarios = $this->User->find('all', $us);