whiteoctober / Pagerfanta

Pagination for PHP.
Other
1.59k stars 2 forks source link

Is order should be matter for setMaxPerPage? And why if so? #227

Closed mikulov closed 7 years ago

mikulov commented 7 years ago

For example, This won't work, default maxPerPage will not be overridden:

    if ($result->success == true) {
        $adapter = new FixedAdapter($result->items_count, $result->items);
        $pagination = new Pagerfanta($adapter);
     $pagination->setCurrentPage($page_number);
     $pagination->setMaxPerPage($pages_limit);

This will work fine:

    if ($result->success == true) {
        $adapter = new FixedAdapter($result->items_count, $result->items);
        $pagination = new Pagerfanta($adapter);
     $pagination->setMaxPerPage($pages_limit);
     $pagination->setCurrentPage($page_number);

Defaults in pagerfanta.php:

   /**
     * @param AdapterInterface $adapter An adapter.
     */
    public function __construct(AdapterInterface $adapter)
    {
        $this->adapter = $adapter;
        $this->allowOutOfRangePages = false;
        $this->normalizeOutOfRangePages = false;
        $this->maxPerPage = 10;
        $this->currentPage = 1;
    }
stof commented 7 years ago

the issue you have is that by default, out of range pages are not allowed, and this is validated when calling setCurrentPage. So if the page size is not yet configured with your expected value at this time, the validation may consider the page out of range while it is actually not with smaller pages. You indeed need to configure the page size before the current page if you want to keep the validation for out of range pages.