感觉就像我已经尝试过一切所以我现在来找你。
我正在尝试订购我的数据,但它不是那么顺利,对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
我该如何解决这个问题?创建的字段显然存在于数据库中。 :/
答案 0 :(得分:8)
使用多个过滤器时需要传递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;
添加到控制器的顶部。
答案 1 :(得分:4)
尝试
$this->set('threads', $this->paginate('Thread', array(
'Thread.hidden' => 0,
'Thread.forum_category_id' => $id
),
array(
'Thread.created' => 'desc'
)
));
我不是蛋糕大师,只是一个猜测。
EDIT。恩,那就对了。蛋糕manual摘录:
控制用于订购的字段
...
$this->paginate('Post', array(), array('title', 'slug'));
所以顺序是第三个参数。
答案 2 :(得分:0)
尝试
$all_threads = $this->Threads->find('all',
array(
'order' => 'Threads.created'
)
);
$saida = $this->paginate($all_threads,[
'conditions' => ['Threads.hidden' => 0]
]);
答案 3 :(得分:0)
有几点需要注意的地方。对于Cake 3.x,您需要:
1)确保已将字段包括在“ sortWhitelist”中
$this->paginate = [
'sortWhitelist' => [
'hidden', 'forum_category_id',
],
];
2)表示“订单”,如果将其放在$ this-> paginate下,则无法在视图中对该字段进行排序。因此最好将“ order”放入查询中(遗憾的是,文档中并未对此进行说明)
$query = $this->Thread->find()
->where( ['Thread.hidden' => 0, 'Thread.forum_category_id' => $id, ] )
->order( ['Thread.created' => 'desc'] );
$this->set('threads', $this->paginate($query)