如何用s括号将sql中的查询重写为symfony

时间:2014-02-16 11:43:34

标签: sql symfony doctrine-orm

我试着写这样的东西:

 $em = $this->getEntityManager();
        $subQuery = $em->createQueryBuilder();
        $subQuery->expr()->exists("SELECT r1.id
                                   FROM Entity\\Role r1
                                   LEFT JOIN u.roles
                                   WHERE r1.name = 'ROLE_A'");

        $subQuery2 = $em->createQueryBuilder();
        $subQuery2->expr()->exists("SELECT r2.id
                               FROM Entity\\Role r2
                               LEFT JOIN u.roles
                               WHERE r2.name = 'ROLE_D'");

        $subQuery3 = $em->createQueryBuilder();
        $subQuery3->expr()->orX($subQuery->getDql(), $subQuery2->getDql());


        $queryBuilder = $em->createQueryBuilder();
        $queryBuilder
            ->select('u')
            ->from('Entity\\User', 'u')
            ->from('Entity\\UserSeason', 'us')
            ->where('u.id = us.user')
            ->andWhere('us.season = :season')
            ->andWhere('us.department = :department')
            ->andWhere($subQuery3->getDql());
        $queryBuilder->setParameter('season', $season);
        $queryBuilder->setParameter('department', $department);

但它不起作用......我想要的是这样的想法:

        SELECT u
        FROM Entity\\User u
             Entity\\UserSeason us
        WHERE u = us.user
        AND us.season = :season
        AND us.department = :department
        AND (
           EXISTS SELECT r1.id
                   FROM Entity\\Role r1
                   LEFT JOIN u.roles
                   WHERE r1.name = 'ROLE_A'
            OR EXISTS SELECT r2.id
                       FROM Entity\\Role r2
                       LEFT JOIN u.roles
                       WHERE r2.name = 'ROLE_D'
        )

我尝试了一些方法,但我找不到合适的人......

有什么想法吗?感谢

0 个答案:

没有答案