简体   繁体   English

使用带有Symfony2和Doctrine中的多对多关系的QueryBuilder创建查询

[英]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.

相关问题 Symfony2和Doctrine:使用查询生成器进行多对多关系 - Symfony2 and Doctrine: Many-to-many relation using query builder 使用QueryBuilder在ZF2和Doctrine中建立具有多对多关系的查询 - Create a Query using QueryBuilder with a Many-to-Many relation in ZF2 and Doctrine 使用Doctrine和Symfony2查询多对多关系 - Query on a many-to-many relationship using Doctrine with Symfony2 Symfony2如何使用querybuilder加入多对多关系? - Symfony2 how to join on many-to-many relations using querybuilder? Symfony2 / Doctrine不会建立多对多关系 - Symfony2/Doctrine won't create many-to-many relationship 如何使用querybuilder排除不具有多对多关系的实体,以Symfony2形式创建字段? - How can I make a field in a Symfony2 form, using querybuilder, excluding entities without a many-to-many relation? Symfony 2.2 Doctrine2多对多关系插入查询 - Symfony 2.2 Doctrine2 many-to-many relation insert query Symfony2:Doctrine不会以多对多关系加载相关实体 - Symfony2: Doctrine does not load related entities in many-to-many relation Symfony2:如何从Doctrine ArrayCollection中删除元素(多对多关系)? - Symfony2: How to remove an element from a Doctrine ArrayCollection (many-to-many relation)? Symfony2,Doctrine2 - 强制更新 - 表已存在于多对多关系中 - Symfony2, Doctrine2 - force update - table already exists on many-to-many relation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM