简体   繁体   English

原则DQL,将IN语句与实体一起使用

[英]Doctrine DQL, using the IN statement with entities

Is it in Doctrine possible to use the IN statement and pass a list of entities as parameter for the IN statement? 在Doctrine中,是否可以使用IN语句并将实体列表作为IN语句的参数传递?

For example, with with the following relation: 例如,具有以下关系:

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Calendar", mappedBy="education", cascade={"persist"})
 */
private $calendar;

I would like to do a query like: 我想做一个查询:

 SELECT p FROM Education p WHERE p.calendar IN (:calendar)

An as :calendar parameter an array of entities. as:calendar参数是实体数组。

$query->setParameter('calendar', array($singleEntity,$singleEntity2));

But that gives the following error: 但这会产生以下错误:

near 'calendar IN (:calendar)': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField 

You can use full entities for the IN statement and DQL will be like 您可以对IN语句使用完整的实体,DQL就像

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

I have used this before quite a few times if I recall correctly and it always worked. 如果我没记错的话,我已经使用过好几次了,而且它一直有效。 I usually use the JOIN and an attribute like Cerad suggested but this should work too. 我通常使用JOIN和建议的Cerad这样的属性,但这也应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM