Php 细枝中循环内的计数关系

Php 细枝中循环内的计数关系,php,symfony,one-to-many,twig,Php,Symfony,One To Many,Twig,我使用Symfony2和细枝模板。把Q&A和stackoverflow想得一模一样。有一系列问题,包括分数、答案、观点等。如何计算细枝中循环中的问题的答案?问答表之间有很多关系 {% for question in questions %} <li>{{ question.score }}</li> <li>{# there should be the count // count($question->getAnswers()) #}&

我使用Symfony2和细枝模板。把Q&A和stackoverflow想得一模一样。有一系列问题,包括分数、答案、观点等。如何计算细枝中循环中的问题的答案?问答表之间有很多关系

{% for question in questions %}
    <li>{{ question.score }}</li>
    <li>{# there should be the count // count($question->getAnswers()) #}</li>
    <li>{{ question.view }}</li>
{% endfor %}
{%用于问题中的问题%}
  • {{question.score}
  • {#应该有count//count($question->getAnswers())#
  • {{question.view}
  • {%endfor%}

    或者,如果有更好的方法来实现这一点,我愿意接受建议。

    这将打印计数:

    {{ question.answers|length }}
    

    我认为这是关于堆栈溢出的第200000个PHP问题。祝贺是的,我知道:)谢谢!这里写着:过滤器“长度”不存在。我必须在某个地方激活该过滤器吗?使用此语法仍然会抛出查询并将所有文档检索到内存中。我们只需要在Mongodb-ODM中抛出count查询。你知道如何实现吗?