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
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
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