Zend 1 Paginator limit

747 Views Asked by At

I'm working with Zend 1 (1.12), and I'm using the Zend_Paginator class to display query results on a web-page. However, there's a large number of records (over one million), and realistically, in most cases, they're only going to need or want to search through the most recent 1000 or so records.

The problem is, I already have part of the web-page setting the "per page" value, which inherently creates a limit (and offset, if they're no longer on 'page 1' of the search.)

Basically, what I'm asking is, is there any way to tell the Zend_Pagintor "only fetch the first 1000 records, and then paginate them based on the user's per page selection"?

I have tried passing in a limit parameter into the interface I'm using, but the Zend_Paginator seems to ignore that, fetches all results, and then paginates them.

function getRecords($currentPage)
{
    $select = "select * from table where user_id = 1 limit 1000"; 
    /* $select is actually a Zend_Db_Table_Select object, not a string. */
    $paginator = new Zend_Paginator::factory($select);
    $paginator->setItemCountPerPage(25);
    $paginator->setCurrentPageNumber($currentPage);

    return $paginator;
}

(The $select is obviously a placeholder.)

2

There are 2 best solutions below

0
Daniel Gadawski On

Try creating SQL view from your limited query:

CREATE OR REPLACE VIEW table_view AS
SELECT * 
FROM table 
WHERE user_id = 1 
LIMIT 1000

This view will return only rows you want (1000 rows in this case), then switch your Zend_Db_Table_Select to this view and create paginator like your getRecord method does.

0
Max P. On

Set total row count manually

$select = "select * from table where user_id = 1";
$paginator = new Zend_Paginator::factory($select);
$paginator->getAdapter()->setRowCount(1000);
...