Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the in statement in DQL in Doctrine 2.0

Tags:

doctrine-orm

I have the following query that uses an IN statement.

$ids = array(1,2,3); $query = 'select o from Organisation o where o.id in (:ids)'; $this->_entityManager->createQuery($query)             ->setParameter('ids', implode(', ', $ids)) 

Doctrine is not returning any results, I think it is because of something wrong in the conversion that Doctrine does for the passed parameter $ids which is an array.

How to make it work?

like image 381
user760868 Avatar asked May 19 '11 10:05

user760868


2 Answers

Try passing the array itself to ->setParameter(...) instead of imploding it into a string.

like image 76
Jeremy Hicks Avatar answered Sep 19 '22 08:09

Jeremy Hicks


I solved this:

$con = $this->getEntityManager(); $query = $con->createQuery("SELECT cl                             FROM BackendBundle:classifieds cl                              INNER JOIN BackendBundle:locations lo WITH cl.locationId = lo.id                             INNER JOIN BackendBundle:municipality mu WITH lo.municipalId = mu.id                             WHERE cl.verified = false AND mu.id = ".$munId." AND cl.locationId NOT IN (:ids) "); $query->setParameters(array('ids' => $locsIds)); return $query->getResult(); 
like image 37
Fernando León Avatar answered Sep 23 '22 08:09

Fernando León