【问题标题】:Doctrine DQL, using the IN statement with entitiesDoctrine DQL,对实体使用 IN 语句
【发布时间】: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


【解决方案1】:

您可以对 IN 语句使用完整的实体,DQL 就像

$query->select('p')
  ->from('Education', 'p')
  ->where($query->expr()->in('p.calendar', ':values'))
  ->setParameter('values',  array($singleEntity,$singleEntity2));

如果我没记错的话,我之前已经用过很多次了,而且它总是有效的。我通常使用 JOIN 和 Cerad 建议的属性,但这也应该有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多