简体   繁体   中英

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?

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.

$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

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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