Question

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

Was it helpful?

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)');

OTHER TIPS

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top