0

In my Symfony 3.2 project, I want to show all themes which are used in table1 and table2 by user_id = 1.

This is the MySQL-query which works perfect in PhpMyAdmin:

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;

The problem is that I couldn't translate it for my ThemeRepository class in my project... I tried this code:

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();
}

The output is an error: [Syntax Error] line 0, col 131: Error: Unexpected '*'

Can someone help me with my query? Thanks in advance!

Jasper Poppe
  • 481
  • 2
  • 7
  • 16

0 Answers0