Php 子查询不存在
我在存储库中有以下代码:Php 子查询不存在,php,symfony,doctrine-orm,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'
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())));
希望这对我有所帮助,因为我无法发表评论,所以我将发布一个固定版本的GregFire解决方案
$sub = $this->_em->createQueryBuilder();// _em stands for entity manager here. While $qb may use repositories createQueryBuilder() which requires alias
$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())));
$sub=$this->createQueryBuilder()代码>函数的参数太少\\ORM\\EntityRepository::createQueryBuilder(),传入了0个,预期至少有1个
请对您的答案添加一些解释,以便其他人可以从中学习。要比较,运行时不会导致错误。根据我在代码中的注释。实体管理器和ORM实体repo对createQueryBuilder()有不同的定义。这里没有什么可学的,只是一个补丁。