Skip to content
Advertisement

doctrine not exists subquery

I have in a repository this code:

 public function getNotAssignedBy($speciality, $diploma)
{
    $qb = $this->createQueryBuilder('j')
        ->select('DISTINCT(j.id) id', 'j.firstName', 'j.lastName', 'j.dateBirth', 'j.sex')
        ->leftJoin('j.qualifications', 'q')
    ;


    if ($speciality) {
        $qb->andWhere('q.speciality = :speciality_id')->setParameter('speciality_id', $speciality);
    }
    if ($diploma) {
        $qb->andWhere('q.diploma = :diploma_id')->setParameter('diploma_id', $diploma);
    }

    $result = $qb->getQuery()->getResult();

    return $result;
}

How can I get only rows where id not exists in another entity ??

Any help. Thanks

Advertisement

Answer

You can achieve it with something like this:

    ....
    // construct a subselect joined with an entity that have a relation with the first table as example user
    $sub = $this->createQueryBuilder();
    $sub->select("t");
    $sub->from("AnotherEntity","t");
    $sub->andWhere('t.user = j.id');

    // Your query builder:
    $qb->andWhere($qb->expr()->not($qb->expr()->exists($sub->getDQL())));

Hope this help

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement