How to Limit the paginate in cakephp
Asked Answered
E

5

9

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 records per page.
How i can do this in paginate ?

Sample Code:

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

            );
Esplanade answered 27/5, 2011 at 12:45 Comment(0)
E
10

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'];
  }
Exploration answered 27/5, 2011 at 13:23 Comment(5)
I am using order by so i cant give 'Model.id' in conditions... my result need to display only last 25 records according to high 'Profile.winning'Esplanade
Oh, that's a completely different story. Not so easy to do that in Cake as it turns out.Exploration
Edited the answer. Not liking it too much though. It just seems strange that Cake doesn't have a built-in, core solution for that...Exploration
2nd solution works! Just bit inconvenience need to edit both model & controllerMarrero
also unfortunately it must return a totallimit value; and if the records is less than totallimit, the paging still stick to totallimit valueMarrero
M
2

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;
    }
Marrero answered 5/4, 2012 at 1:25 Comment(0)
T
1

Use maxLimit in CakePHP v2.x .

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

read more about it here.

Tourist answered 28/4, 2013 at 7:14 Comment(0)
S
1
$query = $this->User->find('all', [
    'recursive' => 2,
    'order' => array('Profile.winning' => 'DESC'),
    'limit' => 25, 
    'offset' => 50
]);
$this->paginate = array(
    $query,
    'limit' => 5
);
Synsepalous answered 25/11, 2017 at 1:28 Comment(1)
Please add an explanation that explains what this code does to solve the problem.Acroter
G
0

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'));
    }
Guyenne answered 30/12, 2020 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.