doctrine not exists subquery

2019-04-07 07:58发布

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

1条回答
地球回转人心会变
2楼-- · 2019-04-07 08:51

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

查看更多
登录 后发表回答