Symfony2 Twig Get Total Count for Child Entity

前端 未结 2 1905
臣服心动
臣服心动 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 %}
    

提交回复
热议问题