使用 fetch-joined 集合限制学说查询? [英] Limiting a doctrine query with a fetch-joined collection?

查看:10
本文介绍了使用 fetch-joined 集合限制学说查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回博客文章及其评论的学说查询:

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)?

推荐答案

Paginate 已与学说 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 DoctrineORMToolsPaginationPaginator;

编写您的查询然后调用结果.

Write your query then call results like that.

$query->setMaxResults($limit);
$query->setFirstResult($offset);
$results = new Paginator($query, $fetchJoin = true);

希望这会对你有所帮助.

Hope this will help you.

注意:如果您使用的是 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆