我将 Doctrine 2 与我的 Zend Framework 应用程序一起使用,典型的查询结果可能会产生一百万(或更多)搜索结果。
我想使用 Zend_Paginator 来符合这个结果集。但是,我不想将所有结果作为数组返回并使用数组适配器,因为这效率低下,而是我想向分页器提供当时的总行数和基于限制/偏移量的结果数组。
使用数组适配器可以实现这一点吗?还是我需要创建自己的分页适配器?
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?
发布评论
评论(3)
您必须扩展自己的适配器。数组适配器以您不希望的方式工作 - 它接收一个数组并根据当前设置返回其中的一部分。您需要的是一个新的适配器,它将采用 DQL 语句并设置限制/偏移量。
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.
这是http://framework.zend.com/manual/en/ zend.paginator.advanced.html
Here it is http://framework.zend.com/manual/en/zend.paginator.advanced.html
您不需要实现 Zend_Paginator_Adapter_Interface,因为它已经由 Zend_Paginator_Adapter_Iterator 实现了。
相反,可以简单地将 Doctrine 的 Paginator 传递给 Zend_Paginator_Adapter_Iterator,如下所示:
然后,您可以像平常一样在视图脚本中使用分页器。
说明:
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)。
请参阅“Zend Framework:初学者指南”代码中从 Doctrine 1 到 Doctrine 2 的移植,从 Doctrine 1 到 Doctrine: https://github.com/kkruecke/zf-beginners-doctrine2。它包括使用 Zend_Paginator_Adapter_Iterator 和 Doctrine 2' Doctrine\ORM\Tools\Pagination\Paginator 来使用 Zend_Paginator 进行分页的代码。
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:
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.