学说 2 和 zend paginator
我想将学说与 zend_paginator
这里使用一些示例查询:
$allArticleObj = $this->_em->getRepository('文章'); $qb = $this->_em->createQueryBuilder();
$qb->add('select', 'a') ->add('来自', '文章a') ->设置第一个结果(0) ->setMaxResults(5);
有没有示例代码表明我们可以为原则 2 查询生成器编写 zend_paginator 适配器?
i want to use doctrine with zend_paginator
here some example query :
$allArticleObj =
$this->_em->getRepository('Articles');
$qb = $this->_em->createQueryBuilder();$qb->add('select', 'a') ->add('from', 'Articles a') ->setFirstResult(0) ->setMaxResults(5);
is there any example code to show we can write a zend_paginator adapter for doctrine 2 query builder?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要实现 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)。像这样:
然后像平常一样在视图脚本中使用分页器。
说明:
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 进行分页的代码。
代码在这里(尽管它可能不适用于最新的 DoctrineORM 2.2),但示例是有效的: https://github.com/kkruecke/zf-beginners-doctrine2/tree/master/ch7
You don't need to implement Zend_Paginator_Adapter_Interface. It is already implement by Zend_Paginator_Adapter_Iterator.
You can simply pass Doctrines' Paginator to Zend_Paginator_Adapter_Iterator, which you pass to Zend_Paginator. Then you call Zend_Paginator::setItemCountPerPage($perPage) and Zend_Paginator::setCurrentPageNumber($current_page). 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.
Code is here (although it might not all work with latest DoctrineORM 2.2) but the example is valid: https://github.com/kkruecke/zf-beginners-doctrine2/tree/master/ch7
我非常惊讶找到一个不会导致 Doctrine 2 和 ZF1 的大型集合出现性能问题的适配器示例是多么困难。
我的灵魂确实使用
Doctrine\ORM\Tools\Pagination\来自 Doctrine 2.2 的 Paginator
就像 @Kurt 的答案;然而不同之处在于,这将从getItems
返回学说分页器(它本身就是一个\Iterator
),而不会对整个查询结果进行水合。I was very surprised how hard it was to find an adapter example that doesn't cause performance issues with large collections for Doctrine 2 and ZF1.
My soultion does use the the
Doctrine\ORM\Tools\Pagination\Paginator
from Doctrine 2.2 just like @Kurt's answer; however the difference that this will return the doctrine paginator (Which is it's self an\Iterator
) fromgetItems
without the entire query results being hydrated.当然,结果是您必须实现
Zend_Paginator_Adapter_Interface
,这本质上意味着实现两个方法:count()
getItems($offset, $perPage) )
你的适配器将接受 Doctrine 查询作为构造函数参数。
原则上,
getItems()
部分实际上很简单。只需将$offset
和$perPage
限制添加到查询中(就像您在示例中所做的那样),然后执行查询。实际上,
count()
业务往往比较棘手。我将遵循Zend_Paginator_Adapter_DbSelect
的示例,将Zend_Db
操作替换为Doctrine
类似操作。The upshot, of course, is that you have to implement the
Zend_Paginator_Adapter_Interface
, which essentially means implementing the two methods:count()
getItems($offset, $perPage)
Your adapter would accept the Doctrine query as a constructor argument.
In principle, the
getItems()
part is actually straightforward. Simply, add the$offset
and$perPage
restrictions to the query - as you are doing in your sample - and execute the query.In practice, it's the
count()
business that tends to be tricky. I'd follow the example ofZend_Paginator_Adapter_DbSelect
, replacing theZend_Db
operations with theirDoctrine
analogues.请更改
为
Please change
to