[英]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.
我想使用Symfony2框架中的自定义DQL查询来过滤查询的结果。
Here is the state of my database: 这是我的数据库状态:
I've got the SQL Query that returns the wanted result: 我有返回所需结果的SQL查询:
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. 我只是想在我的QuestionRepository中实现它。
Thanks a lot for your next help ! 非常感谢您的下一个帮助!
CloudCompany 云公司
You can achieve this using NOT EXISTS and MEMBER OF. 您可以使用NOT EXISTS和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)');
Thank you for your answer FuzzyTree! 谢谢您的回答FuzzyTree! It works!
有用! I had adapted it in order to put it into my QuestionRepository.
我已经对其进行了修改,以便将其放入我的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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.