$query = $this->Users->Mailboxes->Messages->find();
$query
->where(['Messages.mailbox_id' => $id ])
->select([
'messages_ids' => $query->func()->max('Messages.id'),
])
->group('Messages.user_id');
$query2 = $this->Users->Mailboxes->Messages->find()
->where(['id IN' => $query])
->select(['Messages.id', 'Messages.user_id','Messages.body'])
->order(['created' => 'DESC'])
;
this is what i wanted… got it now.