How to Limit the paginate in cakephp

前端 未结 5 1434
时光说笑
时光说笑 2021-01-17 23:58

How to Limit the paginate in cakephp ?

Assume that i have 400 records.
I need to get only 25 records from 50th record to 75th record and need to display 5 recor

相关标签:
5条回答
  • 2021-01-18 00:11

    Improved version with reference of: http://www.mainelydesign.com/blog/view/best-paginatecount-cakephp-with-group-by-support

    This return the correct total count base on whichever is less.

    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;
        }
    
    0 讨论(0)
  • 2021-01-18 00:13

    In cakephp 4.x version we have to call like below

     public function index()
        {
            $this->loadComponent('Paginator');
            $settings = array(
                'limit' => 50
            );
            $prices = $this->Paginator->paginate($this->Prices->find(), $settings);
            $this->set(compact('prices'));
        }
    
    0 讨论(0)
  • 2021-01-18 00:19
    $query = $this->User->find('all', [
        'recursive' => 2,
        'order' => array('Profile.winning' => 'DESC'),
        'limit' => 25, 
        'offset' => 50
    ]);
    $this->paginate = array(
        $query,
        'limit' => 5
    );
    
    0 讨论(0)
  • 2021-01-18 00:24

    Use maxLimit in CakePHP v2.x .

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

    read more about it here.

    0 讨论(0)
  • 2021-01-18 00:25

    You can set conditions for the pagination.

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

    EDIT:

    A solution from here:

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

    And then in the Model:

    public function paginateCount($conditions = null, $recursive = 0, $extra = array())
      {
      if( isset($extra['totallimit']) ) return $extra['totallimit'];
      }
    
    0 讨论(0)
提交回复
热议问题