我对Symfony框架很陌生。我有如下正常的SQL,这些经过测试并返回结果。但是我需要转换为Symfony。

SELECT *
FROM Car c
WHERE c.status NOT IN ('DELETED', 'HIDE')
AND NOT EXISTS (
        SELECT NULL
        FROM Booking b
        WHERE b.periodStart Between '2018-02-06 10:51:30' And '2018-02-06 12:51:30'
        AND b.status = 'Normal'
        AND b.car_id = c.id
    );


到目前为止,下面是我的Symfony代码。

$query = $this->getDoctrine()->getRepository('AppBundle:Car')
                    ->createQueryBuilder('c')
                    ->where("c.status NOT IN (:DELETE , :HIDE )")
                    ->setParameter('DELETE','DELETED')
                    ->setParameter('HIDE','HIDE')
                    ->getQuery()
                    ->getResult();


查询运行顺利并返回结果。但是我不知道如何在上面的代码中添加“ NOT EXISTS”。需要帮助。

最佳答案

我通读了文档,发现解决方案如下:

$em = $this->getDoctrine()->getManager();
            $query      = $em ->createQuery(
                        'SELECT c from AppBundle:Car c
                         WHERE  c.status NOT IN  (\'DELETED\', \'HIDE\')
                         AND NOT EXISTS (
                            SELECT b
                            FROM AppBundle:Booking b
                            WHERE b.periodStart Between :periodStart And :periodEnd
                            AND b.status = \'Normal\'
                            AND b.car = c.id )')
                        ->setParameter ('periodStart', $currentDateTime)
                        ->setParameter ('periodEnd', $nextTwoHourDateTime);

            $result =$query -> getResult();


希望能为别人解决

关于php - 如何从普通SQL中的NOT EXISTS转换为DOCTRINE ORM,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48634949/

10-16 12:10