【发布时间】:2016-11-08 11:11:12
【问题描述】:
在 Doctrine 中是否可以使用 IN 语句并将实体列表作为 IN 语句的参数传递?
例如,具有以下关系:
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Calendar", mappedBy="education", cascade={"persist"})
*/
private $calendar;
我想做这样的查询:
SELECT p FROM Education p WHERE p.calendar IN (:calendar)
一个 as :calendar 参数是一个实体数组。
$query->setParameter('calendar', array($singleEntity,$singleEntity2));
但这会产生以下错误:
near 'calendar IN (:calendar)': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField
【问题讨论】:
-
对于 DQL(不是 DML?),您需要加入日历: SELECT education FROM EducationClassName education LEFT JOIN education.calendar calendar WHERE calendar.id IN (:calendarIds) 我一直使用 ids for IN声明,所以我知道它们有效,但您可以尝试使用不带 .id 的完整实体,看看会发生什么。
-
@Cerad 已将 DML 更新为 DQL。
-
@Cerad 这正是我使用的解决方法。
标签: php orm doctrine-orm dql