1

I'm trying to order a set of results but for some reason it doesn't get ordered properly. Am i making some kind of a mistake here?

$quotations = $this->Quotations->find()
    ->select(['Quotations.id' , 'Quotations.created' , 
'Quotations.supplier_id' , 'Quotations.approval' , 
        'Quotations.region'  ,'Quotations.ponumber' , 'Suppliers.id' , 
'Suppliers.name'])
    ->leftJoinWith('Suppliers')
    ->order(['Quotations.created' => 'DESC'])
    ->toArray();

Using CakePHP3 3.5.2

Kasun Wijesekara
  • 167
  • 1
  • 1
  • 13
  • Your query is fine, what do you mean by "ordered properly"? is it ordered by Quotations.id DESC instead of Quotations.created DESC? In which type is your created property? – SamHecquet Oct 25 '17 at 15:20
  • Yes it's ordered by Quotations.id DESC – Kasun Wijesekara Oct 25 '17 at 15:25
  • @SamHecquet any idea what's wrong with it? – Kasun Wijesekara Oct 25 '17 at 16:30
  • Sorry, I don't see anything wrong. To help your debugging, you should also replace `toArray()` by `toSql()` and see how the SQL query looks like You should try the same query without the `leftJoinWith`. – SamHecquet Oct 25 '17 at 16:36
  • @SamHecquet ah alright. Thanks for the advice. – Kasun Wijesekara Oct 25 '17 at 16:38
  • Would need to see the output & assuming 'created' is using Cake's automagic TimestampBehavior; My guess is that it works as expected but you're noticing your ID's in perfect descending order from high to low. This makes sense if you're ordering by created DESC since your latest created date will be the highest ID, next record will be the second highest ID, created the second last... And so on – david.philip Nov 02 '17 at 20:12

0 Answers0