Search code examples
cakephpview-helperspaginatorcakephp-2.3

PaginatorHelper is not checking the page number


I have a problem with the Cakephp2.3 paginator helper. In the 1.3 version, the paginate method was checking if the passed page param has a correct value. Now, it's not working anymore.

For example, if I have a list with 30 results, 10 items per page, then this was redirecting to the last (third) page:

mycontroller/index/page:130

Now, when I modify the url directly in the browser and pass an invalid page number, it just says there are no results.

This is how I'm using the paginate method:

$this->paginate = array(
                'limit' => 10,
                'order' => 'Appointment.start_datetime DESC'
            );
$appointments = $this->paginate('Appointment');

Is there something I'm missing?

Thank you in advance.

UPDATE

The paginator helper does know how to count the number of pages corectly, but it doesn't seem to care if a correct page number is passed. Here is the output from the view:

print_r($this->Paginator->params());

shows:

Array
(
    [page] => 1
    [current] => 10
    [count] => 30
    [prevPage] => 
    [nextPage] => 1
    [pageCount] => 3
    [order] => Appointment.start_datetime DESC
    [limit] => 10
    [options] => Array
        (
            [page] => 1
        )

    [paramType] => named
)

Solution

  • It seems that both Cake version include some code that takes care of a too high page parameter:

    Cake 1.3: Controller->paginate()

    if ($page === 'last' || $page >= $pageCount) {
        $options['page'] = $page = $pageCount;
    } elseif (intval($page) < 1) {
        $options['page'] = $page = 1;
    }
    

    Cake 2.3: PaginatorComponent->paginate()

    $pageCount = intval(ceil($count / $limit));
    $page = max(min($page, $pageCount), 1);
    

    But interestingly, in Cake 2.3, this code is placed after the call to find(), so too late to fetch the records correctly. It may be a never signaled bug.