2

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

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

这是我的代码:

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

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

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

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

4

4 回答 4

9

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

这应该这样做:

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

或者更清楚一点:

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

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

于 2012-12-10T20:41:38.407 回答
3

尝试

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

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

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

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

所以顺序是第三个参数。

于 2012-12-10T20:29:05.007 回答
0

尝试

$all_threads = $this->Threads->find('all', 
            array(
                'order' => 'Threads.created'
            )
        );
        $saida = $this->paginate($all_threads,[
            'conditions' => ['Threads.hidden' => 0]
        ]);
于 2018-05-24T04:53:57.757 回答
0

在按顺序进行分页时需要注意一些事项。对于 Cake 3.x,您需要:

1) 确保您已将字段包含在“sortWhitelist”中

$this->paginate = [

        'sortWhitelist' => [
            'hidden', 'forum_category_id', 
        ],


    ];

2) 对于'order',如果你把它放在$this->paginate 下,你将无法在视图中对该字段进行排序。所以最好将“订单”放在查询中(遗憾的是,这在文档中没有说明)

$query = $this->Thread->find()
->where( ['Thread.hidden' => 0, 'Thread.forum_category_id' => $id, ] )
->order( ['Thread.created' => 'desc'] );

$this->set('threads', $this->paginate($query) 
于 2018-07-12T00:36:00.960 回答