Question

I'm trying to use a WHERE ... IN () clause in my DQL, but somehow get stuck. I expect a result of three articles (WHERE a.id IN (1,2,20)), but only get one article (with id=1) as result. The articles definitely exist in that specific team I set as another parameter.

The SQL (I use for $q = $em->createQuery($sql)) looks like this:

SELECT a
FROM AcmeBundle:Article a
WHERE a.team = :team
AND a.id IN (:listOfArticleIds)
ORDER BY a.updated DESC

The DQL ($q->getSql()) it creates is this:

SELECT a0_.id AS id0, a0_.title AS title1, a0_.description AS description2, a0_.status AS status3, a0_.created AS created4, a0_.updated AS updated5, a0_.contributor_id AS contributor_id6, a0_.company_id AS company_id7
FROM Article a0_
WHERE a0_.team_id = ? AND a0_.id IN (?)
ORDER BY a0_.updated DESC LIMIT 10 OFFSET 0

The parameters ($q->getParameters()) look like this

object(Doctrine\Common\Collections\ArrayCollection)#647 (1) {
  ["_elements":"Doctrine\Common\Collections\ArrayCollection":private]=>
  array(2) {
    [0]=>
    object(Doctrine\ORM\Query\Parameter)#646 (3) {
      ["name":"Doctrine\ORM\Query\Parameter":private]=>
      string(4) "team"
      ["value":"Doctrine\ORM\Query\Parameter":private]=>
      int(1)
      ["type":"Doctrine\ORM\Query\Parameter":private]=>
      string(7) "integer"
    }
    [1]=>
    object(Doctrine\ORM\Query\Parameter)#520 (3) {
      ["name":"Doctrine\ORM\Query\Parameter":private]=>
      string(13) "listOfArticleIds"
      ["value":"Doctrine\ORM\Query\Parameter":private]=>
      string(6) "1,2,20"
      ["type":"Doctrine\ORM\Query\Parameter":private]=>
      int(2)
    }
  }
}

Anyone has an idea what might be wrong here? I'm curious about the type of the listOfArticleIds parameter, that is int(2), but I would expect it to be a string. I already manually set it to string ($q->setParameter('listOfArticleIds', $listOfArticleIds, 'string')) but that doesn't change anything.

Was it helpful?

Solution

listOfArticleIds should be an array, like:

$q->setParameter('listOfArticleIds', array(1, 2, 20))

OTHER TIPS

you need to pass the parameter as array and not as string, doctrine will implode it automatically.

I remember it works only with array of integer but i'm not sure, I use querybuilder:).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top