计算树枝中循环内的关系

计算树枝中循环内的关系

本文介绍了计算树枝中循环内的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use Symfony2 and twig templating. Think of the Q&A exact same as stackoverflow. There are list of questions with the count of score, answers, views and so on. How to count the answers of the qeustions inside loop in twig? There are OneToMany relation between Question and Answer tables.

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

或者如果有更好的方法可以做到这一点,我愿意提出建议.

Or if there is any better way to achieve this, I am open to suggestions.

推荐答案

这将打印计数:

{{ question.answers|length }}

这篇关于计算树枝中循环内的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:56