Search code examples
phpsymfonydoctrine-ormdqlquery-builder

Create a Query using QueryBuilder with a Many-to-Many relation in Symfony2 and Doctrine


Good evening everybody!

I've got a little problem. I would like to filter a query's result using a custom DQL Query in Symfony2 framework.

Here is the state of my database:

Extract of the database

I've got the SQL Query that returns the wanted result:

SELECT * FROM question WHERE question_id NOT IN (SELECT question_id FROM questions_joueurs WHERE joueur_id = 1)

I just would like to implement that in my QuestionRepository.

Thanks a lot for your next help !

CloudCompany


Solution

  • You can achieve this using NOT EXISTS and MEMBER OF. Something like this

    $qb->select('q.question_id, q.question_intitule')
        ->from('MyBundleNameSpace\Entity\Question', 'q')
        ->where('NOT EXISTS (SELECT 1 FROM MyBundleNameSpace\Entity\Jouer j WHERE j MEMBER OF q.jouers)');