Symfony 3.2使用2个子查询创建查询

时间:2017-04-13 23:17:29

标签: mysql symfony repository symfony-3.2 createquery

在我的Symfony 3.2项目中,我希望显示themestable1table2 = 1使用的所有user_id

这是在PhpMyAdmin中完美运行的MySQL查询:

SELECT DISTINCT themes.*
FROM themes
WHERE EXISTS
    (SELECT *
     FROM table1
     WHERE table1.theme_id = themes.id
     AND table1.user_id = 1)
OR EXISTS
    (SELECT *
     FROM table2
     WHERE table2.theme_id = themes.id
     AND table2.user_id = 1)
ORDER BY themes.name;

问题是我无法在我的项目中为我的ThemeRepository类翻译它...我尝试了这段代码:

public function findAllUsedByUserId($userId) {
    return $this->getEntityManager()
        ->createQuery('
            SELECT DISTINCT t
            FROM AppBundle:Theme t
            WHERE EXISTS
                (SELECT *
                FROM AppBundle:Table1 a
                WHERE a.theme_id = t.id
                AND a.user_id = :userId)
            OR EXISTS
                (SELECT *
                FROM AppBundle:Table2 b
                WHERE b.theme_id = t.id
                AND b.user_id = :userId)
            ORDER BY t.name ASC
        ')
        ->setParameter('userId', $userId)
        ->getResult();
}

输出错误:[Syntax Error] line 0, col 131: Error: Unexpected '*'

有人可以帮助我查询吗?提前谢谢!

0 个答案:

没有答案