3

我有一个表,我想首先获取所有等于 26 的 id,然后将其余的按降序排序,例如:

row    id
---    --

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

通常会导致:

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

find()我应该如何在蛋糕中构建一个?这就是我的想法:

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

但是我应该如何告诉 Cake 检索其余的 id 并在检索到所有等于 26 的 id 后按降序对它们进行排序?

4

2 回答 2

5

尝试这个。

$this->Model->find('all', array(
  'order' => array('Model.id = 26 DESC' , 'Model.id DESC')
));
于 2013-06-10T12:53:03.223 回答
0

假设您的id字段是 CakePHP 约定所定义的 primaryKey,那么您的查询将只返回一个结果。因此这里的排序是不相关的。

我还建议使用find('first')以防止您获得以数字方式索引的单个结果。

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

如果您想返回这一条记录,然后是所有其他记录,我会用find('all').

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

于 2013-06-10T09:26:59.607 回答