我有以下使用IN语句的查询。
$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没有返回任何结果,我认为这是因为Doctrine对传递的参数$ids
进行的转换有问题。
如何让它发挥作用?
答案 0 :(得分:46)
尝试将数组本身传递给->setParameter(...)
,而不是将其插入字符串中。
答案 1 :(得分:1)
我解决了这个问题:
$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();
答案 2 :(得分:0)
我用过这个(setParameter似乎对我不起作用):
$em->createQuery('SELECT users FROM Entities\User users WHERE users.id IN (:ids)')
->setParameters(array('ids' => $ids));
http://redbeardtechnologies.wordpress.com/2011/07/01/doctrine-2-dql-in-statement/
答案 3 :(得分:-1)
我正在努力使用IN语句,使用$ query-> expr() - > in()构造......
尝试:
$em->createQuery(“SELECT users FROM Entities\User users WHERE users.id IN (':ids')”)
->setParameters(array(‘ids’ => $ids));
我认为IN()部分中参数的简单引用是必要的......