Skip to content
Advertisement

One particular id at first and after all ids in desc order

I need to get one particular id at first and, after that, all ids in descending order. This is what I have done but when I uncomment that order line it won’t work,

$result = $this->Practicetestanswers->find('all',
    ['contain' => ['Practicetestquestions' =>
        ['Practicetestconceptassignment' => ['Practicetestconcept', 'Practicetest']
        ]]])
    ->where(['Practicetestanswers.user_id' => $student_id,
        'Practicetestquestions.is_active' => 'y',
        'Practicetest.is_active' => 'y'
    ])
    ->select([
        'id' => 'Practicetest.id',
        'test_code' => 'Practicetest.test_code',
        'test_title' => 'Practicetest.test_title',
        'time_taken' => 'SUM(Practicetestanswers.time_taken)',
    ])
    ->group('Practicetest.id')
  //    ->order(["FIELD(Practicetest.id, '228')" => 'DESC'])   //need like this
        ->order(["Practicetest.id" => 'DESC'])
        ->hydrate(false)
        ->toArray();

Advertisement

Answer

Just replace this line with your order by condition

->order(['FIELD(Practicetest.id, "228")'=> 'DESC', "Practicetest.id" => 'DESC' ])
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement