I’ve encountered a problem while trying to fetch some information that I thought should be there between related entities in Symfony2 with Doctrine.
Let’s say I have an entity called “Publisher” which haves many “Magazines” which have many “Chapters” which have many “Pages”, and I want to fetch, from one “Publisher” object get the total amount of Pages that all their Magazines, through chapters, have.
Whil trying to count the Magazines, directly related, I have no problem (I’m using Twig as template).
In the controller i retrieve the list:
$entities = $em->getRepository('Bundle:Publisher')->findAll();
And in the view I can render this:
{{ entity.magazines.count }}
Which gives me the Magazines related to that publisher. But when i try to do the following:
{{ entity.magazines.chapters.count }}
(Assuming “Chapters” is another entity related ManyToOne Magazine) it throws an error.
I know I’m missing something here but I cannot figure out what it is. Could someone help me or give a tip on how to do this?
You can’t do that in this way. You should use the DQL instead.
At now after accessing
entity.magazinesyou get a Collection of Magazine Entities and you should loop this collection to access thechaptersof the each items in theentity.magazinescollectionSo see the
Using Aggregate Functionshere