学说额外的懒惰负载不能像预期的那样工作

学说额外的懒惰负载不能像预期的那样工作

本文介绍了学说额外的懒惰负载不能像预期的那样工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体 Shop 和相关实体 ShopProduct ,具有以下关系:

  / ** 
* @ ORM\OneToMany(targetEntity =ShopProduct,mappedBy =shopid ,fetch =EXTRA_LAZY)
* /
private $ products;

在twig模板中,我想访问 $ c>产品,所以我访问

  {{entity.getProducts()。count}} 

但是,当使用symfony2分析器查看查询的数量和内容时,我看到一个完整的选择是发出的,而不是 COUNT 我会期望(基于)。



发送每个 Shop 的内存使用量为250Mb +,页面加载时间为30+秒,这是不需要的。



添加 fetch =EXTRA_LAZY我已经清除了学说缓存。



我是否忽略某些东西,使用错误的方法或误解文档? / p>

  doctrine / annotations v1.1 
doctr ine / cache v1.0
原则/集合v1.1
原则/常见2.4.0-RC1
原则/数据夹具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


解决方案

刚刚遇到同样的问题,解决方案非常简单:



{{value.getAlerts.count()}}



而不是



{{value.getAlerts.count}}



Twig必须覆盖Doctrine count()方法意味着Extra Laz y加载一个变体,它是自己的实现,只是愚蠢地获取所有的实体来对它们进行计数。



将所有预期的SELECT *查询转换为COUNT(*)...


I have an entity Shop and a related entity ShopProduct, with the following relation:

/**
 * @ORM\OneToMany(targetEntity="ShopProduct", mappedBy="shopid", fetch="EXTRA_LAZY")
 */
private $products;

In a twig template I want to access the count of products, so I access

{{ entity.getProducts().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).

Issuing the full select for each of the Shops results in a memory usage of 250Mb+ and a page load time of 30+ seconds, which is undesired.

After adding the fetch="EXTRA_LAZY" I've cleared the doctrine caches.

Am I overlooking something, using the wrong approach or misunderstanding the docs?

[edit]

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() }}

instead of

{{ value.getAlerts.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.

That turned all expected SELECT * queries into COUNT(*)...

这篇关于学说额外的懒惰负载不能像预期的那样工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:15