简体   繁体   中英

Zend_Paginator / Doctrine 2

I'm using Doctrine 2 with my Zend Framework application and a typical query result could yield a million (or more) search results.

I want to use Zend_Paginator in line with this result set. However, I don't want to return all the results as an array and use the Array adapter as this would be inefficient, instead I would like to supply the paginator the total amount of rows then and array of results based on limit/offset amounts.

Is this doable using the Array adapter or would I need to create my own pagination adapter?

You will have to extend your own adapter. The Array adapter works the way you don't want - it receives an array and return a portion of it based on the current settings. What you'll need is a new adapter that will take DQL statemenet and set the limit/offset.

You don't need to implement Zend_Paginator_Adapter_Interface , as it is already implement by Zend_Paginator_Adapter_Iterator .

Instead can simply pass Doctrine's Paginator to Zend_Paginator_Adapter_Iterator like this:

use Doctrine\ORM\Tools\Pagination as Paginator; // goes at top of file

SomeController::someAction() 
{
 $dql = "SELECT s, c FROM Square\Entity\StampItem s JOIN s.country c ".' ORDER BY '.  $orderBy . ' ' . $dir;

 $query = $this->getEntityManager()->createQuery($dql);
 $d2_paginator = new Paginator($query); 

 $d2_paginator_iter = $d2_paginator->getIterator(); // returns \ArrayIterator object

 $adapter =  new \Zend_Paginator_Adapter_Iterator($d2_paginator_iter);

 $zend_paginator = new \Zend_Paginator($adapter);          

 $zend_paginator->setItemCountPerPage($perPage)
            ->setCurrentPageNumber($current_page);

 $this->view->paginator = $zend_paginator; 
}

Then you use paginator in the view script just like you ordinarly do.

Explanation:

Zend_Paginator 's constructor can take a Zend_Paginator_Adapter_Interface , which Zend_Paginator_Adpater_Iterator implements. Now, Zend_Paginator_Adapter_Iterator 's constructor takes an \\Iterator interface. This \\Iterator must also implement \\Countable (as you can see by looking at Zend_Paginator_Adapter_Iterator 's constructor). Since Paginator::getIterator() method returns an \\ArrayIterator , it by definition it fits the bill (since \\ArrayIterator implements both \\Iterator and \\Countable ).

See this port from Doctrine 1 to Docrine 2 of the code for "Zend Framework: A Beginner's Guide" from Doctrine 1 to Doctrine: https://github.com/kkruecke/zf-beginners-doctrine2 . It includes code for paginating with Zend_Paginator using Zend_Paginator_Adapter_Iterator with Doctrine 2' Doctrine\\ORM\\Tools\\Pagination\\Paginator .

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