将查询从MySQL转换为Doctrine(使用NOT IN)

时间:2015-07-03 10:31:17

标签: mysql symfony doctrine-orm doctrine

我需要帮助将MySQL转换为Doctrine查询。

有一个查询我要翻译的内容:

SELECT izo_order.id 
FROM izo_order 
WHERE izo_order.id 
NOT IN (SELECT izo_installation.order_id 
        FROM izo_installation 
        WHERE izo_installation.order_id=izo_order.id)
        AND izo_order.client_id = 4

我做了类似的事情,但似乎不起作用:

$qb = $this->_em->createQuery('SELECT o
FROM AppBundle:Order o
WHERE o.id
NOT IN (SELECT i.order
        FROM AppBundle:Installation i
        WHERE i.order=o.id)
        AND o.clientBelongsTo = :client_id')->setParameter('client_id', $client_id);

        return $qb->getResult();

1 个答案:

答案 0 :(得分:0)

您的查询看起来不错,但子查询中的Where子句应该导致错误。不用那样试试吧:

$qb = $this->_em->createQuery('SELECT o
FROM AppBundle:Order o
WHERE o.id
NOT IN (SELECT IDENTITY(i.order)
    FROM AppBundle:Installation i)
    AND o.clientBelongsTo = :client_id')->setParameter('client_id', $client_id);
$qb->getResult();

如果您仍有错误,请发布错误。