【问题标题】:How to Limit the paginate in cakephp如何在 cakephp 中限制分页
【发布时间】:2011-09-03 09:22:09
【问题描述】:

如何在 cakephp 中限制分页?

假设我有 400 条记录。
我只需要从第 50 条记录到第 75 条记录中获取 25 条记录 并且需要每页显示 5 条记录。
我如何在分页中做到这一点?

示例代码:

        $this->paginate = array(
                'contain'=>array('User'),
                'recursive' => 2,
                'order' => array('Profile.winning' => 'DESC'),
                'limit' =>5

            );

【问题讨论】:

    标签: cakephp cakephp-1.3 cakephp-1.2


    【解决方案1】:

    您可以设置分页条件。

    function listRecords()
      {
      $this->paginate = array(
        'conditions' => array('Model.id >=' => 50, 'Model.id <=' => 75),
        'limit' => 5
        );
      $this->paginate('Model');
      );
    

    编辑:

    来自here的解决方案:

    $this->paginate = array(
      'limit' => 20,
      'totallimit' => 1000
      );
    

    然后在模型中:

    public function paginateCount($conditions = null, $recursive = 0, $extra = array())
      {
      if( isset($extra['totallimit']) ) return $extra['totallimit'];
      }
    

    【讨论】:

    • 我正在使用 order by,所以我无法在条件下提供 'Model.id'...我的结果需要根据高 'Profile.winning' 仅显示最后 25 条记录
    • 哦,那是完全不同的故事。事实证明,在 Cake 中做到这一点并不容易。
    • 编辑了答案。不过也不是很喜欢。 Cake 没有内置的核心解决方案,这似乎很奇怪......
    • 第二个解决方案有效!只是有点不便需要编辑模型和控制器
    • 不幸的是它必须返回一个总限制值;如果记录小于totallimit,分页仍然坚持totallimit值
    【解决方案2】:

    参考的改进版本:http://www.mainelydesign.com/blog/view/best-paginatecount-cakephp-with-group-by-support

    这将返回正确的总计数,以较小者为准。

    public function paginateCount($conditions = null, $recursive = 0, $extra = array()) 
        {
            $conditions = compact('conditions');
            if ($recursive != $this->recursive) {
                $conditions['recursive'] = $recursive;
            }
            unset( $extra['contain'] );
            $count = $this->find('count', array_merge($conditions, $extra));
    
            if (isset($extra['group'])) {
                $count = $this->getAffectedRows();
            }
    
            if (isset($extra['totallimit']) && $extra['totallimit'] < $count) {
                return $extra['totallimit'];
            }
    
            return $count;
        }
    

    【讨论】:

      【解决方案3】:

      在 CakePHP v2.x 中使用 maxLimit

      public $paginate = array(
          // other keys here.
          'maxLimit' => 10
      );
      

      阅读更多关于它的信息here

      【讨论】:

        【解决方案4】:
        $query = $this->User->find('all', [
            'recursive' => 2,
            'order' => array('Profile.winning' => 'DESC'),
            'limit' => 25, 
            'offset' => 50
        ]);
        $this->paginate = array(
            $query,
            'limit' => 5
        );
        

        【讨论】:

        • 请添加解释说明此代码如何解决问题。
        【解决方案5】:

        在 cakephp 4.x 版本中,我们必须像下面这样调用

         public function index()
            {
                $this->loadComponent('Paginator');
                $settings = array(
                    'limit' => 50
                );
                $prices = $this->Paginator->paginate($this->Prices->find(), $settings);
                $this->set(compact('prices'));
            }
        

        【讨论】:

          猜你喜欢
          • 2016-12-21
          • 2014-05-31
          • 2023-03-11
          • 2022-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-02
          • 1970-01-01
          相关资源
          最近更新 更多