CakePHP分页和顺序

Ms0*_*s01 2 php cakephp

感觉就像我已经尝试过一切所以我现在来找你.

我正在尝试订购我的数据,但它不是那么顺利,对Cake来说有点新鲜.

这是我的代码:

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

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

AND `Thread`.`forum_category_id` = 12 AND order = ('desc') ORDER BY `Thread`.`created` ASC LIMIT 25
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?创建的字段显然存在于数据库中.:/

Ros*_*oss 9

conditions使用多个过滤器时需要传入密钥(即订单,限制......).如果指定条件,则可以直接将其作为第二个参数传递.

这应该这样做:

$this->set('threads', $this->paginate('Thread', array(
        'conditions' => array(
            'Thread.hidden' => 0,
            'Thread.forum_category_id' => $id
        ),
        'order' => array(
            'Thread.created' => 'desc'
        )
    )));
Run Code Online (Sandbox Code Playgroud)

或者更清楚一点:

$this->paginate['order'] = array('Thread.created' => 'desc');
$this->paginate['conditions'] = array('Thread.hidden' => 0, ...);
$this->paginate['limit'] = 10;
$this->set('threads', $this->paginate());
Run Code Online (Sandbox Code Playgroud)

如果出现错误,请添加public $paginate;到控制器的顶部.