php - symfony2.1 : count doctrine collection in twig template

标签 php templates doctrine-orm twig symfony-2.1

我有一个包含实体集合()的学说实体。 现在我想计算实体并打印出计数。 像这样:

<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/

相关文章:

php - Google/YouTube Insight 样式 "Interactive PNGs"?

php - 使用 foreach 语句更新 MySQL 中的相同输出

php - 一段时间后自动从数据库中的表中删除数据

php - 如何根据用户在 symfony2 中提交的数据进行条件表单验证?

C++ 模板元编程 : Inheritance from template template parameter

c++ - 如何对通过几层模板派生的类型进行typedef?

c++ - 在 C++11 中的编译时 (constexpr) 计算斐波那契数(递归方法)

php - 教义刷新更改并释放内存

symfony - 在symfony(Doctrine)中更新实体时图片被删除

php - Symfony2 : Duplicate definition of column 'id' on entity in a field or discriminator column mapping