1

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

2 Answers 2

3

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)');
Sign up to request clarification or add additional context in comments.

Comments

1

Thank you for your answer FuzzyTree! It works! I had adapted it in order to put it into my QuestionRepository.

Here is my method:

public function findNotAnsweredByJoueurs(Partie $partie, $level)
{
    $qb = $this->createQueryBuilder('q');
    $qb->where('q.level < ' . $level);
    foreach($partie->getJoueurs() as $joueur)
    {
        $qb->andWhere('NOT EXISTS (SELECT ' . $joueur->getId() . ' FROM Cloud\Bundle\MoneyDropBundle\Entity\Joueur j WHERE j MEMBER OF q.joueurs)');
    }

    return $qb->getQuery()
              ->getResult();
}

1 Comment

The foreach isn't necessary in fact you're overwriting the where condition with each iteration (to stack where conditions use 'andWhere' after the first 'where').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.