【问题标题】:CakePHP paginate and order byCakePHP 分页和排序
【发布时间】:2018-07-12 08:36:00
【问题描述】:

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

我正在尝试订购我的数据,但进展不顺利,对 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

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

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    在使用多个过滤器(即订单、限制...)时,您需要传入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; 添加到控制器顶部。

    【讨论】:

    • 刚刚意识到公共分页已经设置在顶部并带有订单声明。让我自己瞎了一阵子。谢谢。
    【解决方案2】:

    试试

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

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

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

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

    所以顺序是第三个参数。

    【讨论】:

    • 查询运行,但没有排序:/
    • @cubsink 你能看看新生成的 SQL 吗?
    • 接受了你的回答,因为它是正确的,你是第一个 :)
    • 答案有点错误。第三个参数是whitelist,因此您只能在分页助手中指定几个可以通过名为参数sort 排序的字段。但是实际的排序参数仍然需要在选项数组(第二个参数)或 $this->pagination['Model']['order'] 中设置
    【解决方案3】:

    试试

    $all_threads = $this->Threads->find('all', 
                array(
                    'order' => 'Threads.created'
                )
            );
            $saida = $this->paginate($all_threads,[
                'conditions' => ['Threads.hidden' => 0]
            ]);
    

    【讨论】:

      【解决方案4】:

      在按顺序进行分页时需要注意一些事项。对于 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) 
      

      【讨论】:

        猜你喜欢
        • 2012-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-10
        • 2012-03-08
        • 2013-09-28
        • 2012-08-16
        • 2016-06-12
        相关资源
        最近更新 更多