简体   繁体   中英

Zend 2 Paginator how does it work?

I'm trying to understand the Zend Paginator and would mostly like to make sure it doesn't break my scripts.

For example, I have the following snippet which successfully loads some contacts one at a time:

$offset = 1;
//returns a paginator instance using a dbSelect;
$contacts = $ContactsMapper->fetchAll($fetchObj);

$contacts->setCurrentPageNumber($offset);
$contacts->setItemCountPerPage(1);

$allContacts = count($contacts);
while($allContacts >= $offset) {
    foreach($contacts as $contact) {
        //do something
    }
    $offset++;
    $contacts->setCurrentPageNumber($offset);
    $contacts->setItemCountPerPage(1);
}

However I can have hundreds of thousands of contacts in the database and matched by the SELECT I send to the paginator. Can I be sure it only loads one at a time in this example? And how does it do it, does it run a customized query with limit and offset ?

From the official documentation : Zend Paginator Usage

Note


Instead of selecting every matching row of a given query, the DbSelect adapter retrieves only the smallest amount of data necessary for displaying the current page. Because of this, a second query is dynamically generated to determine the total number of matching rows.

If your using Zend\\Paginator\\Adapter\\DbSelect it will apply limit and offset to the query you're passing it, and it will just fetch the wanted records. This is done in the getItems() function of DbSelect , you could see that these lines in the source code.

You could also read this from the documentation :

This adapter does not fetch all records from the database in order to count them. Instead, the adapter manipulates the original query to produce a corresponding COUNT query. Paginator then executes that COUNT query to get the number of rows. This does require an extra round-trip to the database, but this is many times faster than fetching an entire result set and using count() , especially with large collections of data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM