Doctrine查询只获得许多相关的“第一”(做连接和选择以避免N + 1)

问题描述 投票:0回答:1

在这种情况下,我有3个相关的实体profile --OneToMany - > annualReports - ManyToOne - > photo

在一个循环中,对于一张桌子,我正在检查这样的树枝中是否存在照片

{% set is_photo = profile.annualReports.toArray()[0].photo.id is defined %}

所以,一个qazxsw poi可以有很多qazxsw poi - 这种特殊情况我只想要第一个“报告”。

问题是如何在profile中执行此操作?即加入“第一个报告”(“第一个”可以基于最小ID或创建日期)

更新:我想问题是我怎么能'限制1'并指定排序?

symfony doctrine
1个回答
1
投票

如果你想在twig中获得第一个报告,你只需要使用Collection类的annualReports方法:

QueryBuilder

并且要管理订单,您必须使用学说注释'@OrderBy':first

{% if profile.annualReports IS NOT EMPTY %}
   {% set report = profile.annualReports.first %}
{% endif%}

© www.soinside.com 2019 - 2024. All rights reserved.