Subquery in doctrine2 notIN Function

后端 未结 3 799
甜味超标
甜味超标 2020-11-30 06:09

I\'d like to select members who are not in specific service. I have 3 tables :

  • membre
  • service
  • membre_service<
相关标签:
3条回答
  • 2020-11-30 06:13

    You can use (NOT) MEMBER OF:

    <?php
    $query = $em->createQuery('SELECT m.id FROM Custom\Entity\Membre WHERE :service NOT MEMBER OF m.services');
    $query->setParameter('service', $service);
    $ids = $query->getResult();
    

    See the documentation for more examples.

    0 讨论(0)
  • 2020-11-30 06:14

    The same alias cannot be defined 2 times in the same query

    $qb  = $this->_em->createQueryBuilder();
    $qb2 = $qb;
    $qb2->select('m.id')
        ->from('Custom\Entity\MembreService', 'ms')
        ->leftJoin('ms.membre', 'm')
        ->where('ms.id != ?1');
    
    $qb  = $this->_em->createQueryBuilder();
    $qb->select('mm')
        ->from('Custom\Entity\Membre', 'mm')
        ->where($qb->expr()->notIn('mm.id', $qb2->getDQL())
    );
    $qb->setParameter(1, $service);
    $query  = $qb->getQuery();
    
    return $query->getResult();
    

    Ideally you should use many-to-many relation for your entity, in this case your query is going to be much simpler.

    0 讨论(0)
  • 2020-11-30 06:27

    Actually if you are using the Symfony2 repository class you could also do the following:

    $in = $this->getEntityManager()->getRepository('Custom:MembreService')
        ->createQueryBuilder('ms')
        ->select('identity(ms.m)')
        ->where(ms.id != ?1);
    
    $q = $this->createQueryBuilder('m')
        ->where($q->expr()->notIn('m.id', $in->getDQL()))
        ->setParameter(1, $service);
    
    return $q->getQuery()->execute();
    
    0 讨论(0)
提交回复
热议问题