1

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

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

通过增加顺序来检索我的所有章节,但 CakePHP 似乎忽略了“顺序”参数。我相信我的语法是正确的(基于视图源: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

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

作为一种变通方法,我很乐意在模型上放置一个 order 属性。由于我通常想按序列号检索章节,因此可以将其添加到模型中:

public $order = 'Chapter.sequence_number ASC';

一旦我这样做了,蛋糕就会生成

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

1 回答 1

2

如果你尝试这样的事情,而不是:

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

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

于 2013-04-08T16:35:33.047 回答