Query Order By Was Error from my controller function

I have a function from my controller that showing this code

$pklSchedule = $this->PklSchedules->get($id, [
     'contain' => ['Students' =>function($q){
          return $q->contain(['industries', 'levels']);

     }], 
 ]);

 $this->set('pklSchedule', $pklSchedule);

it creates the query

SELECT 
  Students.id AS `Students__id`, 
  Students.nis AS `Students__nis`, 
  Students.name AS `Students__name`, 
  Students.created AS `Students__created`, 
  Students.modified AS `Students__modified`, 
  Students.level_id AS `Students__level_id`, 
  Students.schedule_id AS `Students__schedule_id`, 
  Students.industry_id AS `Students__industry_id`, 
  Industries.id AS `Industries__id`, 
  Industries.name AS `Industries__name`, 
  Industries.holder_name AS `Industries__holder_name`, 
  Industries.address AS `Industries__address`, 
  Industries.created AS `Industries__created`, 
  Industries.modified AS `Industries__modified`, 
  Industries.major_id AS `Industries__major_id`, 
  Levels.id AS `Levels__id`, 
  Levels.name AS `Levels__name`, 
  Levels.created AS `Levels__created`, 
  Levels.modified AS `Levels__modified`, 
  Levels.major_id AS `Levels__major_id` 
FROM 
  students Students 
  LEFT JOIN industries Industries ON Industries.id = (Students.industry_id) 
  INNER JOIN levels Levels ON Levels.id = (Students.level_id) 
WHERE 
  Students.schedule_id in (2)

and I’m stack on my function that I hope can make a query like this

SELECT 
  Students.id AS `Students__id`, 
  Students.nis AS `Students__nis`, 
  Students.name AS `Students__name`, 
  Students.created AS `Students__created`, 
  Students.modified AS `Students__modified`, 
  Students.level_id AS `Students__level_id`, 
  Students.schedule_id AS `Students__schedule_id`, 
  Students.industry_id AS `Students__industry_id`, 
  Industries.id AS `Industries__id`, 
  Industries.name AS `Industries__name`, 
  Industries.holder_name AS `Industries__holder_name`, 
  Industries.address AS `Industries__address`, 
  Industries.created AS `Industries__created`, 
  Industries.modified AS `Industries__modified`, 
  Industries.major_id AS `Industries__major_id`, 
  Levels.id AS `Levels__id`, 
  Levels.name AS `Levels__name`, 
  Levels.created AS `Levels__created`, 
  Levels.modified AS `Levels__modified`, 
  Levels.major_id AS `Levels__major_id` 
FROM 
  students Students 
  LEFT JOIN industries Industries ON Industries.id = (Students.industry_id) 
  INNER JOIN levels Levels ON Levels.id = (Students.level_id) 
WHERE 
  Students.schedule_id in (2) ORDER BY Student.industry_id DESC

anyone can help to change my function above that can looks like the second query
(ORDER BY Student.industry_id DESC)

Does return $q->contain(['industries', 'levels'])->order(['Student.industry_id' => 'DESC']) work? I’m not sure whether it will, or if the order clause will need to be added somewhere else in the query.

1 Like

thank you for your help, i have just confused where i can put the order function

You can put it on any query, to order the results of that particular query.