计算枝条内循环内的关系

sef*_*rov 15 php one-to-many symfony twig

我使用Symfony2和twig模板.想想问答与stackoverflow完全相同.有一些问题列表,包括得分,答案,观点等.如何计算树枝内循环中qeustions的答案?问答表之间存在OneToMany关系.

{% for question in questions %}
    <li>{{ question.score }}</li>
    <li>{# there should be the count // count($question->getAnswers()) #}</li>
    <li>{{ question.view }}</li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

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

Oha*_*has 30

这将打印计数:

{{ question.answers|length }}
Run Code Online (Sandbox Code Playgroud)