Write “NOT IN” in Doctrine Query Language

前端 未结 2 1043
无人及你
无人及你 2021-01-20 02:55

I have two tables company(id, ...) and company_has_wtax(company_id, ....). I need to get all companies that are not in company_has_wtax

相关标签:
2条回答
  • 2021-01-20 03:00

    Try this :

    $em = $this->getDoctrine()->getManager();
    
    $query = $em->createQuery('SELECT * FROM YourBundle\Entity\Company c WHERE c.id NOT IN (SELECT c2.company_id FROM YourBundle\Entity\Company_has_wtax c2)');
    
    $companies = $query->getResult(); // array of Company objects
    
    0 讨论(0)
  • 2021-01-20 03:07

    Try this:

    $q2 = $this->createQueryBuilder('c')
        ->select('IDENTITY(c2.company)')
        ->join('RegisterCompanyBundle:CompanyHasMedia', 'c2', 'WITH', 'c2.company = c.id');
    
    $query = $this->createQueryBuilder('c3');
    $query->where($query->expr()->notIn('c3.id', $q2->getDQL()));
    
    $companies = $query->getQuery()->getResult();
    

    Please pay attention that we created query from a none related entity by reversedBy/mappedBy
    We need to use IDENTIY for the specific field of the related table

    0 讨论(0)
提交回复
热议问题