Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doctrine extra lazy load doesn't work as expected with 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
like image 997
The Rookie Avatar asked Mar 26 '13 19:03

The Rookie


1 Answers

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(*)...

like image 194
Vigintas Labakojis Avatar answered Sep 20 '22 15:09

Vigintas Labakojis