Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 603
Yssn Avatar asked Jul 21 '15 10:07

Yssn


1 Answers

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

like image 91
Matteo Avatar answered Sep 20 '22 18:09

Matteo