Symfony2 Twig Get Total Count for Child Entity

前端 未结 2 1873
臣服心动
臣服心动 2021-01-22 13:47

The following entities exist, Farm, Barn and Animals. A Farm can have many Barns and a Barn many Animals.

When displaying a Farm in a TWIG template the number of Animals

相关标签:
2条回答
  • 2021-01-22 14:27

    Use Entity accessors :

    {% for farm in farms %}
    
      {{ farm.name }}
    
      {% set barns = farm.getBarns() %}
    
      Barns count = {{ barns|length }}
    
      {% for barn in barns %}
    
        {% set animals = barn.getAnimals() %}
    
        {{ barn.name }} animals count : {{ animals|length }}
    
      {% endfor %}
    
    {% endfor %}
    
    0 讨论(0)
  • 2021-01-22 14:27

    You are looking for the length filter

    When used with an array, length will give you the number of items. So, if your array is farm.barns, you can just use {{ farm.barns|length }}

    0 讨论(0)
提交回复
热议问题