php - 学说不存在子查询

标签 php symfony doctrine-orm

我在存储库中有这段代码:

 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;
}

我怎样才能只获取另一个实体中不存在 id 的行??

任何帮助。谢谢

最佳答案

你可以用这样的东西来实现它:

    ....
    // 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())));

希望对你有帮助

关于php - 学说不存在子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31536137/

相关文章:

php - 快速实体主义水化器

php - 如何通过 SyliusResourceBundle 使用 Sylius 创建新模型

使用 apachelounge VC14 构建和 php.net VC11 构建时,php dll 无法加载到服务器中

php - trigger_error 是否中断脚本?

css - 在页面完全加载之前元素出现在错误的位置

Symfony2 注册表单 - 使用 jQuery 额外创建的字段不会保留在数据库中,仅来自上次输入的值

mapping - 如何使用 Doctrine 2 管理 VIEW?

javascript - 打印外部页面而不打开它

php - 如何使用 PHPseclib 捕获未处理的错误?

静态方法上的 PHP call_user_func