I have similar issue.
My Users has many Bets, Bets belongTo Matches, Matches has TeamAs and TeamBs.
Now I want my Users View page to display all the Matches that user has bets , sorting by Matches.Match_Time
I tried - worked fine, but of course no sorting
$user = $this->Users->get($id, [
'contain' => ['Bets.Matches.TeamAs' , 'Bets.Matches.TeamBs']]);
Below also works fine, but no sorting as well
$user = $this->Users->find('all', [
'conditions' => ['id' => $id],
'contain' => ['Bets' => ['Matches' => ['TeamAs' , 'TeamBs']]] ])
->first();
After adding order clause, it says unknown column Matches.Match_Time
$user = $this->Users->find('all', [
'conditions' => ['id' => $id],
'contain' => ['Bets' => ['Matches' => ['TeamAs' , 'TeamBs']]],
'order' => ['Matches.Match_Time' => 'DESC']
])->first();
Then I tried your approach, now page can not even load, it said: An Internal Server Error occured
$user = $this->Users->find('all', [
'conditions' => ['id' => $id],
'contain' => ['Bets' => ['Matches' => ['queryBuilder' => function (Query $q) {
return $q->order(['Matches.Match_Time' => 'DESC']);
}]]]
])->first();
Please help, thank you very much.