findall忽略顺序参数?

时间:2013-04-08 16:28:12

标签: cakephp

我正在尝试使用以下内容:

    $this->Chapter->recursive=1;
    $chaps = $this->Chapter->find('all', array(
         'order'=> array('sequence_number' => 'ASC') 
    ));
    $this->set('chapters', $chaps );

通过增加顺序检索我的所有章节,但CakePHP似乎忽略了'order'参数。我相信我的语法是正确的(基于view-source:http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#creating-custom-find-types,它表示以下内容应该有效:

public function index() {
    $articles = $this->Article->find('available', array(
        'order' => array('created' => 'desc')
    ));
}

)。该表的SQL如下所示:

CREATE TABLE chapters (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    sequence_number INT UNSIGNED,
    title VARCHAR(50)
);

我没有得到任何语法或运行时错误。但是,Cake生成的实际获取章节记录的SQL是:

SELECT `Chapter`.`id`, `Chapter`.`sequence_number`, `Chapter`.`title` 
FROM `Tutorial`.`chapters` AS `Chapter` WHERE 1 = 1

显然我做错了什么,但我不知道它是什么。

作为解决方案我很乐意在模型上添加订单属性。由于我通常希望按序列号检索章节,所以我可以将其添加到模型中:

public $order = 'Chapter.sequence_number ASC';

我做了那个Cake生成

SELECT `Chapter`.`id`, `Chapter`.`sequence_number`, `Chapter`.`title` 
FROM `Tutorial`.`chapters` AS `Chapter` WHERE 1 = 1 
ORDER BY `Chapter`.`sequence_number` ASC

1 个答案:

答案 0 :(得分:2)

如果您尝试这样的事情,那该怎么办:

$chaps = $this->Chapter->find('all', array(
     'order' => array('Chapter.sequence_number ASC') 
));

这部分的主要区别在于:Chapter.sequence_number ASC