学说额外的延迟加载无法按预期使用计数 [英] doctrine extra lazy load doesn't work as expected with count
问题描述
我有一个实体Shop
和一个相关实体ShopProduct
,关系如下:
I have an entity Shop
and a related entity ShopProduct
, with the following relation:
/**
* @ORMOneToMany(targetEntity="ShopProduct", mappedBy="shopid", fetch="EXTRA_LAZY")
*/
private $products;
在树枝模板中,我想访问 products
的 count,所以我访问
In a twig template I want to access the count of products
, so I access
{{ entity.getProducts().count }}
但是当使用 symfony2 分析器查看查询的数量和内容时,我看到发出了一个完整的选择,而不是我期望的 COUNT
(基于 文档).
But when using the symfony2 profiler to look at number and content of the queries, I see that a full select is issued, instead of the COUNT
I would expect (based on the documentation).
为每个 Shop
发出完全选择会导致 250Mb+ 的内存使用和 30+ 秒的页面加载时间,这是不希望的.
Issuing the full select for each of the Shop
s results in a memory usage of 250Mb+ and a page load time of 30+ seconds, which is undesired.
添加 fetch="EXTRA_LAZY"
后,我已经清除了学说缓存.
After adding the fetch="EXTRA_LAZY"
I've cleared the doctrine caches.
我是否忽略了某些内容、使用了错误的方法或误解了文档?
Am I overlooking something, using the wrong approach or misunderstanding the docs?
doctrine/annotations v1.1
doctrine/cache v1.0
doctrine/collections v1.1
doctrine/common 2.4.0-RC1
doctrine/data-fixtures dev-master eef10f6
doctrine/dbal 2.3.3
doctrine/doctrine-bundle v1.2.0-beta1
doctrine/doctrine-fixtures-bundle dev-master 275540d
doctrine/doctrine-migrations-bundle dev-master 99c0192
doctrine/inflector v1.0
doctrine/lexer v1.0
doctrine/migrations dev-master e1f6efc
doctrine/orm 2.3.3
推荐答案
刚遇到同样的问题,解决方法很简单:
Just encountered the same problem and the solution was very simple:
{{ value.getAlerts.count() }}
而不是
{{ value.getAlerts.count }}
Twig 必须用它自己的实现的变体覆盖 Doctrine count()
方法,该方法旨在额外延迟加载",只是愚蠢地获取所有实体来计算它们.
Twig must be overriding the Doctrine count()
method meant to "Extra Lazy Load" with a variant of it's own implementation that just dumbly fetches all entities to count them.
这将所有预期的 SELECT * 查询变成了 COUNT(*)...
That turned all expected SELECT * queries into COUNT(*)...
这篇关于学说额外的延迟加载无法按预期使用计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!