I have two tables `company(id, ...)` and `company_has_media(company_id, 
....)`. I need to get all companies that are not in `company_has_media` 
table. In raw SQL should be something like:

    SELECT * FROM company c WHERE c.id NOT IN (SELECT chm.company FROM 
company_has_media chm WHERE chm.company = c.id)

I found a way to get this working but it's ugly to me and also something 
say me is not right doing in this way:

    public function findCompanyByDocument() {
        $sql = "SELECT * FROM company c WHERE c.id NOT IN (SELECT 
chm.company FROM company_has_media chm WHERE chm.company = c.id)";
        $em = $this->getEntityManager();

        return $em->getConnection()->fetchAll($sql);
    }

I'm testing this other way:

    public function findCompanyWithoutDocument() {
        $q2 = $this->createQueryBuilder('c2')
                ->select('c2.company')
                ->from('RegisterCompanyBundle:CompanyHasMedia', 'c2');

        $query = $this->createQueryBuilder('c')
                ->from('RegisterCompanyBundle:Company', 'c')
                ->where($query->expr()->notIn('c.id', $q2->getSQL()
        ));

        echo $query->getQuery()->getSQL();
    }

But I get this error:

  ContextErrorException: Notice: Undefined variable: query in 
/var/www/html/kraken/src/Company/RegisterCompanyBundle/Entity/Repository/CompanyRepository.php
 
line 42
Any help or advice?

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to