CakePHP 分页和排序

2021-12-21 00:00:00 php cakephp

感觉我已经尝试了一切,所以我现在来找你.

It feels like I've tried everything so I now come to you.

我正在尝试订购我的数据,但进展不顺利,对 Cake 来说有点新.

I am trying to order my data but it isn't going so well, kinda new to Cake.

这是我的代码:

$this->set('threads', $this->paginate('Thread', array(
        'Thread.hidden' => 0,
        'Thread.forum_category_id' => $id,
        'order' => array(
            'Thread.created' => 'desc'
        )
    )));

它生成一个 SQL 错误,这是最后一个有趣的部分:

It generates an SQL error and this is the last and interesting part:

AND `Thread`.`forum_category_id` = 12 AND order = ('desc') ORDER BY `Thread`.`created` ASC LIMIT 25

我该如何解决这个问题?创建的字段显然存在于数据库中.:/

How can I fix this? The field created obviously exists in the database. :/

推荐答案

尝试

$this->set('threads', $this->paginate('Thread', array(
        'Thread.hidden' => 0,
        'Thread.forum_category_id' => $id
    ),
    array(
        'Thread.created' => 'desc'
    )
));

我不是蛋糕大师,只是猜测.

I'm not a Cake master, just a guess.

编辑.是的,这是正确的.蛋糕 手册 摘录:

EDIT. Yes, thats right. Cake manual excerpt:

控制用于排序的字段...$this->paginate('Post', array(), array('title', 'slug'));

Control which fields used for ordering ... $this->paginate('Post', array(), array('title', 'slug'));

所以顺序是第三个参数.

So order is the third argument.

相关文章