3

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?

dcd0181
  • 1,493
  • 4
  • 29
  • 52

2 Answers2

5

Try this.

$this->Model->find('all', array(
  'order' => array('Model.id = 26 DESC' , 'Model.id DESC')
));
cartina
  • 1,409
  • 13
  • 21
0

Assuming that your id field is your primaryKey as defined by the CakePHP conventions, the query you have will only ever return one result. Thus the ordering is not relevant here.

I would also suggest using find('first') to prevent you getting a single result indexed numerically.

$this->Model->find('first', array('conditions' => array('id' => $id)));

If you wanted to return this single record, and then all the other records, I would inverse this with a find('all').

$this->Model->find('all', array('conditions' => array('id !=' => $id)));

David Yell
  • 11,756
  • 13
  • 61
  • 100