We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Pagination Methodology

Hello

I have used the Paginator in a number of places and I think the current implementation is not ideal.

For example, you have to query the whole set of data and pagniate through it. In case you have southands of items, then you are going to query all of them whenever you access a page that shows them.

A better aproach would be querying the only items required for the current page, and how do you calculate the pages? You can feed the total number of all items to the paginator, the paginator itself should be responsible only for handling the pagination, but not the data itself.

We can use LIMIT in MySQL or skip in MongoDB to limit the queryed data.

What do you think?

Regards

What do you think about something simple like this:

<?php
$page_num = $this->request->get('page'); 

// Default the page to 0 if no page number is passed so it will start at 0 and get 100 records,
// Page 1 would start at 100 and get 100 records, etc.
$page_num = (!$page_num) ? 0 : (int) $page_num;

$per_page = 100;
$offset = (int) ($per_page * $page_num);
$items = \YourModel::find([
        "limit" => [“number” => $per_page, “offset” => 0]
]);

$this->view->setVars(['items' => $items]);


8.7k

That's good, but you still don't have appropriate handling of creating the pagination links, right?