学说+树枝,如何显示学说收集在一个限制的一多关系


Doctrine + Twig, How to display Doctrine collection in a OneToMany relationship with a limit?

我与两个实体有一个OneToMany关系:

一个Article有一个Brand,一个Brand有多个Articles。如何在我的Twig模板中,我只能显示每个Brand的5个Articles ?

轻查询的最佳实践是什么?

下面,我的代码:
{% for brand in colBrands %}
        {% for article in brand.articles %}
            <!-- Display only 5 results and stop -->
        {% endfor %}
{% endfor %}

使用切片函数:

{% for brand in colBrands %}
        {% for article in brand.articles|slice(0, 5) %}
            <!-- Display only 5 results and stop -->
        {% endfor %}
{% endfor %}