How to use zend paginate without loading all database results

So, the way I see zend paginate work is what you do:

$paginator = Zend_Paginator::factory($results);
$paginator->setItemCountPerPage($itemCount);
$paginator->setPageRange($pageRange);

Where $ results is the result of loading a bunch of elements from the database in the form

"SELECT * FROM table WHERE etc"

Then zend paginate will count the number of results and automatically generate page ranges ...

But if you do this, you will need to get all the database results that I see as waste, since only one page is displayed at a time, so you only need to select the elements for this page ...

how do you do zend paginate to calculate the correct page ranges and numbers without having to retrieve all the results of the whole table?

+5
5

factory, Zend_Db_Select Zend_Db_Table_Select. extends Zend_Db_Table_Abstract, , . , , . :

    $adapter = new Zend_Paginator_Adapter_DbSelect($db->select()->from('posts'));
    $adapter->setRowCount(
        $db->select()
           ->from(
                'item_counts',
                array(
                   Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN => 'post_count'
                )
             )
    );

$paginator = new Zend_Paginator($adapter)

http://framework.zend.com/manual/en/zend.paginator.usage.html#zend.paginator.usage.dbselect

+4

Zend_Paginator, , , : SELECT * FROM table WHERE etc LIMIT 0, 30, , , 30 , SELECT * FROM table WHERE etc LIMIT 30, 60

, , Framework, , ( , , ), , , mysql, Paginator , , - , .

0

zend paginator -. . " DbSelect DbTableSelect", , .

0

, . Zend_Paginator_Adapter_Array, zend paginator .. . Zend_Paginator_Adapter_DbSelect, zend paginator select count(*) ...... as zend_paginator_row_count , .

0

Zend_Db_Select and Zend_Db_Table_Select are great ways to solve your problem, a problem that I had to deal with only six months ago.

Suppose I have a book table in my database, and I defined a search function to allow me to perform a full-text search on all fields of the table:

Default_Model_DbTable_Books.php:

public class Default_Model_DbTable_Books extends Zend_Db_Table_Abstract {

    // Other variables and methods here...

    public function search($query) {
        // Initialize Zend_Db_Table_Select object
        $select = $this->select(true);

        if (isset($query) && is_string($query)) {
            // Logic for search (don't actually do this, it horrible for performance)
            $db = $this->getAdapter();
            $where = $db->quoteInto('isbn LIKE ? OR name LIKE ? OR author LIKE ?', $query);
            $search->where($where)
                   ->order('date_published DESC')
                   ->group('author');
        }

        return $select;
    }
}

Default_Model_Mapper_Book.php:

public class Default_Model_Mapper_Book {

    // Defined functions as per Zend Quickstart Mapper classes...

    public function search($query) {
        // Returns Zend_Db_Table_Select object
        return $this->getDbTable()->search($query);
    }
}

Default_BooksController.php:

public function listAction() {
    $mapper = new Default_Model_Mapper_Book();
    $select = $mapper->search($this->_getParam("query"));
    $paginator = Zend_Paginator::factory($select);
}
0
source

All Articles