Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP SQL conversion - basic ORDER BY

I have a table where I'd like to get all id's that equal 26 first, then have the rest sorted in descending order, so something like:

row    id
---    --

1      26
2      26
3      26
4      27
5      25
6      24

Would normally result in:

select id 
from table 
order by id=26 desc, id desc

How should I construct a find() in Cake? This is what I figure:

$this->Model->find('all', array(
    'conditions' => array('Model.id' => 26),
    'order' => array('Model.id' => 'DESC')
));

But how should I tell Cake to retrieve the rest of the id's and sort them in descending order after retrieving all id's that equal 26?

like image 410
dcd0181 Avatar asked Sep 24 '26 17:09

dcd0181


1 Answers

Try this.

$this->Model->find('all', array(
  'order' => array('Model.id = 26 DESC' , 'Model.id DESC')
));
like image 161
cartina Avatar answered Sep 26 '26 06:09

cartina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!