0

I'm not sure if this is possible:

I have deep relations between several entities, and I don't know how to efficiently count the related entities, to show the cumulative totals for each one.

  • I have a "Magazine".
  • A "Magazine" have several "Issues".
  • An "Issue" have several "Chapters".
  • A "Chapter" have several "Pages".
  • And so on...

I want to display something like:

Magazine A: 6 issues, 12 chapters, 24 pages
Magazine B: 12 issues, 20 chapters, 51 pages

So, when retrieving a "Magazine":

$entities = $em->getRepository('MyBundle:Magazine')->findBy(array(),
    array('name' => 'ASC'));

In the template I can do this:

<td>{{ entity.issues.count }}</td>

To show the number of "Issues" related to that "Magazine".

My problem is that now I want the cumulative number of "Chapters" that these subset of "Issues" have, and so on. Something like:

<td class="counter">{{ entity.issues.chapters.count }}</td>
<td class="counter">{{ entity.issues.chapters.pages.count }}</td>

Obviously this doesn't work, and I can't figure out a way to do this. Is there a way to do this?

Ikzer
  • 527
  • 11
  • 29

2 Answers2

0

Try with

{{ entity.issues|length }}
hous
  • 2,577
  • 2
  • 27
  • 66
  • No, the first part works fine. With {{ entity.issues.count }} I can obtain that "6 issues", what I can't have is the {{ entity.issues.chapters.count }}, since {{entity.issues}} doesn't hold, I think, the count of chapters. – Ikzer Aug 06 '14 at 01:58
  • lokk at this http://stackoverflow.com/questions/24454839/symfony2-twig-get-total-count-for-child-entity – hous Aug 06 '14 at 02:22
0

{{ entity.issues.chapters.pages.count }} won't work, because entity.issues is an Array Collection. You can get a chapters of issue like this:

{{ entity.issues[INDEX_KEY].chapters.pages.count }}

or

{% for issue in entity.issues %}
    {{ issue.chapters.count }}
{% endfor %}
xurshid29
  • 4,172
  • 1
  • 20
  • 25