Search code examples
doctrinerepositorysymfonyquery-builderdql

create a specific query with querybuilder in symfony


i'm developing an application with symfony 3 and i want to create a custom query with query builder.
i have an entity named: Bien having many adresses.( Address is an other entity). so i want to get all adresses not existing in Bien entity.

the query that i want to produce is :
select a.id from address a where a.id not in ( select b.address_id from bien b)

in my AddressRepository i did that :

public function getAdressesByRueNotJoined($rue)
    {
        $qb2 = $this->createQueryBuilder('ab')
            ->from('BienBundle:Bien', 'bi');
        $qb = $this->createQueryBuilder('a');

        return
            $qb->where('a.rue = :rue')
                ->setParameter('rue', $rue)
                ->andWhere(
                    $qb->expr()->notIn('a', $qb2->getDQL())
                )
                ->getQuery()
                ->getResult();
    }

the query returned is :

SELECT a0_.id AS id_0, a0_.name AS name_1, a0_.rue_id AS rue_id_2 FROM adresse a0_ WHERE a0_.rue_id = ? AND a0_.id NOT IN (SELECT a1_.id FROM adresse a1_, bien b2_)

How can i resolve that ?


Solution

  • in $qb2 i added identiy like this

    $qb2 = $this->createQueryBuilder('ad')
                ->from('SBC\BienBundle\Entity\Bien', 'bi')
                ->select('IDENTITY(bi.address)');
    

    but i dont what does it mean