我与两个实体有一个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 %}