Write “NOT IN” in Doctrine Query Language

前端 未结 2 1060
无人及你
无人及你 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: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

提交回复
热议问题