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 Shop
s 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(*)...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With