如何限制cakephp中的分页

如何限制cakephp中的分页,cakephp,cakephp-1.3,cakephp-1.2,Cakephp,Cakephp 1.3,Cakephp 1.2,如何限制cakephp中的分页 假设我有400条记录。 从第50条记录到第75条记录,我只需要25条记录 并且每页需要显示5条记录。 我怎样才能在paginate中做到这一点 示例代码: $this->paginate = array( 'contain'=>array('User'), 'recursive' => 2, 'order' => array('P

如何限制cakephp中的分页

假设我有400条记录。
从第50条记录到第75条记录,我只需要25条记录 并且每页需要显示5条记录。
我怎样才能在paginate中做到这一点

示例代码:

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

            );

您可以为分页设置条件

function listRecords()
  {
  $this->paginate = array(
    'conditions' => array('Model.id >=' => 50, 'Model.id <=' => 75),
    'limit' => 5
    );
  $this->paginate('Model');
  );
然后在模型中:

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

改进版,参考:

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

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;
    }
public函数paginateCount($conditions=null,$recursive=0,$extra=array())
{
$conditions=紧凑型(“条件”);
如果($recursive!=$this->recursive){
$conditions['recursive']=$recursive;
}
未结算($extra['contain']);
$count=$this->find('count',array_merge($conditions,$extra));
如果(isset($extra['group'])){
$count=$this->getAffectedRows();
}
如果(isset($extra['totallimit'])和&$extra['totallimit']<$count){
返回$extra['totallimit'];
}
返回$count;
}

在CakePHP v2.x中使用
maxLimit

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

阅读更多信息。

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

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

我正在使用order by,因此我无法在条件中提供“Model.id”。。。我的结果只需要显示最近25张高姿态的记录。获胜是一个完全不同的故事。事实证明,在蛋糕上做这件事并不容易。编辑了答案。不过我不太喜欢。看起来很奇怪,Cake没有内置的核心解决方案……第二个解决方案有效!只需编辑模型和控制器,不幸的是,它必须返回一个totallimit值;如果记录数小于totallimit,则分页仍坚持totallimit值。请添加解释,说明此代码如何解决此问题。
$query = $this->User->find('all', [
    'recursive' => 2,
    'order' => array('Profile.winning' => 'DESC'),
    'limit' => 25, 
    'offset' => 50
]);
$this->paginate = array(
    $query,
    'limit' => 5
);
 public function index()
    {
        $this->loadComponent('Paginator');
        $settings = array(
            'limit' => 50
        );
        $prices = $this->Paginator->paginate($this->Prices->find(), $settings);
        $this->set(compact('prices'));
    }