我有一个包含实体集合(子)的学说实体。 现在我想计算实体并打印出计数。 像这样:
<div class="item">
<h1>{{ object.name }}</h1>
<div class="childrenCount">children {% count (object.children) %}</div>
</div>
我发现了一些不起作用的示例(like using a "count" filter 导致“找不到过滤器”错误)。
最佳答案
如发现here ,对于学说,在处理学说集合时可以选择使用“计数”方法。否则,您可以使用“长度”过滤器。
示例代码:
<ul class="summary">
<li> {{ object.children | length }}</li>
<!-- or, use the count method of doctrine collections directly -->
<li> {{ object.children.count }}</li>
</ul>
关于php - symfony2.1 : count doctrine collection in twig template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13528472/