你不需要实现Zend_Paginator_Adapter_Interface。Zend_Paginator_Adapter_Iterator已经实现了它。
您可以简单地将 Doctrines 的Paginator传递给Zend_Paginator_Adapter_Iterator,然后将其传递给Zend_Paginator。然后调用Zend_Paginator::setItemCountPerPage($perPage)和Zend_Paginator::setCurrentPageNumber($current_page)。像这样:
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 in your view, use it just like your currently use
}
然后像平常一样在视图脚本中使用分页器。
解释:
Zend_Paginator的构造函数可以采用Zend_Paginator_Adapter_Interface,它是Zend_Paginator_Adpater_Iterator实现的。现在,Zend_Paginator_Adapter_Iterator的构造函数采用了一个\Iterator接口。这个\Iterator还必须实现\Countable (通过查看Zend_Paginator_Adapter_Iterator的构造函数可以看到)。由于Paginator::getIterator()方法返回一个\ArrayIterator,因此根据定义它符合要求(因为\ArrayIterator实现了\Iterator和\Countable)。
请参阅从 Doctrine 1 到 Doctrine 的“Zend 框架:初学者指南”代码从 Doctrine 1 到 Docrine 2 的移植: https ://github.com/kkruecke/zf-beginners-doctrine2 。它包括使用Zend_Paginator_Adapter_Iterator和 Doctrine 2' Doctrine\ORM\Tools\Pagination\Paginator与Zend_Paginator进行分页的代码。
代码在这里(尽管它可能不适用于最新的 DoctrineORM 2.2)但示例是有效的:https ://github.com/kkruecke/zf-beginners-doctrine2/tree/master/ch7