Rods
October 3, 2023, 6:30am
1
Which means the 50 results I want to sort it to DESC then to ASC
$data = $this->Messages->find(‘all’)
->where([
‘messages.channel_id =’ => $channelId
])
->order([‘messages.created’ => ‘DESC’, ‘messages.id’ => ‘ASC’],true)
->limit(50);
return $this->response->withType(‘application/json’)
->withStringBody(json_encode($data));
Zuluru
October 3, 2023, 1:55pm
2
I think all your references to messages
should be Messages
instead?
Rods
October 4, 2023, 1:31am
3
yeah, But the order didt’n follow the ASC order. I want the id of 36 should start at the top
Zuluru
October 4, 2023, 3:23am
4
You’ve asked it to sort first by descending order of created timestamp, and then by id. The id sort will be used only if it finds two with the same creation time.
1 Like
Zuluru
October 4, 2023, 3:30am
5
Oh, you want the most recent 50, but sorted by increasing id? What you have will clearly not do that. Might have to do something like
$data = $this->Messages->find(‘all’)
->where(['Messages.channel_id' => $channelId])
->order(['Messages.created' => 'DESC'])
->limit(50)
->sortBy('id', SORT_ASC, SORT_STRING)
->toArray();
1 Like
Rods
October 4, 2023, 4:59am
6
Thanks Zuluru but I also tried the sortBy but It didn’t work