使用fetch-joined集合限制一个原则查询? [英] Limiting a doctrine query with a fetch-joined collection?
问题描述
我有一个教条查询返回博客文章及其评论:
I have a doctrine query that returns blog posts and their comments:
SELECT b, c FROM BlogPost b LEFT JOIN b.comments c
我想将结果限制为10个博文。根据DQL文档, setMaxResults()
在获取加入集合的查询(在本例中为注释))无法正常工作:
I would like to limit the results to 10 blog posts. According to the DQL documentation, setMaxResults()
doesn't work correctly on queries that fetch-join a collection (comments in this case):
如果您的查询包含一个fetch-joined
集合,指定结果限制
方法不能像
所期望的那样工作。设置最大结果限制
数据库结果行的数量
然而,在fetch-joined
集合的情况下,一个根实体可能
出现在许多行中,实际上是
水合少于指定的
结果数。
If your query contains a fetch-joined collection specifying the result limit methods are not working as you would expect. Set Max Results restricts the number of database result rows, however in the case of fetch-joined collections one root entity might appear in many rows, effectively hydrating less than the specified number of results.
如何正确地限制包含fetch-joined的原则查询收集(在这种情况下,将结果限制为10个博客帖子)?
How would I properly limit a doctrine query that contains a fetch-joined collection (in this case, limit the results to 10 blog posts)?
推荐答案
分页符合原则2.2和新的symfony2版本2.0.10兼容。
Paginate was merged with doctrine 2.2 And the new symfony2 release 2.0.10 is compatible with.
现在使用它像
//use Doctrine paginator
use Doctrine\ORM\Tools\Pagination\Paginator;
编写您的查询,然后调用结果。
Write your query then call results like that.
$query->setMaxResults($limit);
$query->setFirstResult($offset);
$results = new Paginator($query, $fetchJoin = true);
希望这将有助于您。
注意:如果您使用的是SF2 2.0.10,则应更新deps和deps.lock文件,并为Doctrine软件包指定2.2版本。
Note: If you are using SF2 2.0.10, you should update the deps and deps.lock files and specify the 2.2 version for Doctrine bundles.
这篇关于使用fetch-joined集合限制一个原则查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!