无论我做什么,我都无法尊重我指定的顺序。
$this->paginate = array(
'Car' => array(
'limit' => 6,
'order' => array(
'Car.year' => 'desc'
),
'table' => 'cars'
)
);
生成的SQL:
SELECT `Car`.`id`, ... `Car`.`year`,... FROM `cars` AS `Car` WHERE 1 = 1 LIMIT 6
答案 0 :(得分:5)
原来我在我的url参数中使用了:sort。一旦我把它拿出来一切都很好:)
答案 1 :(得分:1)
如果您只有一个项目,则不需要/不应该使用数组:
//one thing
var $order = "Model.field DESC";
//multiple things
var $order = array("Model.field" => "asc", "Model.field2" => "DESC");
答案 2 :(得分:0)
另请注意,在分页时默认会忽略虚拟字段。
请参阅Cake 2.0 Pagination文档中的“控制用于排序的字段”。
示例:
$this->MyModel->virtualFields['count'] = 0;
$this->Paginator->settings = array(
'fields' => 'COUNT(id) AS MyModel__count',
'group' => ('MyModel.group_id'),
'order' => array('MyModel__count' => 'DESC'),
);
// IMPORTANT: pass sortable fields including the virtual field as 3rd parameter:
$log = $this->Paginator->paginate('MyModel', null, array('MyModel__count', 'id'));