My used CakePHP Version is 3.x i need cakePHP to create this sql statement:
SELECT messages.id, message, receiver_id, sender_id, conversation_id, users.id, users.username FROM
(
SELECT messages.id, message, receiver_id, sender_id, conversation_id
FROM messages as messages
ORDER BY created DESC
) AS messages
left join users on (users.id = sender_id)
WHERE (sender_id = 13 or receiver_id =13)
GROUP BY conversation_id;
I used the queryBuilder but i do not really understand what im doing ;)
This is my cake code:
$subquery = $messages->find()
->order(['Messages.created' => 'DESC']);
debug($neededMessages = $messages->find('all')
->select()
->group('Messages.conversation_id')
->contain(['Users'])
->where(['OR' => ['sender_id' => $userId, ['receiver_id' => $userId]]]));
$neededMessages->execute();
I think its only a little mistake in there.
Cakes debug output is this:
SELECT (SELECT Messages.id AS Messages__id, Messages.subject AS Messages__subject, Messages.message AS Messages__message, Messages.created AS Messages__created, Messages.conversation_id AS Messages__conversation_id, Messages.is_new AS Messages__is_new, Messages.receiver_id AS Messages__receiver_id, Messages.sender_id AS Messages__sender_id FROM messages Messages ORDER BY Messages.created DESC) FROM messages Messages INNER JOIN users Users ON Users.id = (Messages.sender_id) WHERE (sender_id = :c0 OR receiver_id = :c1) GROUP BY Messages.conversation_id
im missing the columns in the first select statement. Can anyone help on this issue? grretings maik...
Messages.id AS Messages__idwhich will cause the outer select ofMessages.idto fail. Maybe ask ORM wizard and part time leprechaun tamer jose_zap on IRC if he knows a workaround. - ndmselect * from messages, join users on (...) where ... GROUP BY conversation_id ORDER BY created DESC- José Lorenzo RodrÃguez