PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, December 31, 2021

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

 December 31, 2021     cakephp-3.0, mysql, php     No comments   

Issue

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();

Solution

Just replace this line with your order by condition

->order(['FIELD(Practicetest.id, "228")'=> 'DESC', "Practicetest.id" => 'DESC' ])


Answered By - Priyanka Hazra
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing