我有以下表结构
表:医生
id name
1 Mac
2 Smit
表:组织
id name
1 Org1
2 Org2
表:org_doctors
id org_id doctor_id
1 1 1
2 1 2
表:专业化
id name
1 ENT
2 Cardiac
表:doc_specialzations
id doctor_id specialzation_id
1 1 1
2 1 2
3 2 1
以下是我正在处理的学说代码:
$qb = $this->entityManager->createQueryBuilder();
$qb2 = $qb;
$qb2->select('dsp.doctor_id')
->from('Doctor\Entity\DoctorSpecialization', 'dsp')
->where('dsp.specialization = :specializationId')
->setParameter('specializationId', $searchBy['specialization']);
$qb->select('od', 'd', 'o', 'u')
->from('Doctor\Entity\OrgDoctor', 'od')
->leftJoin('od.organization', 'o')
->leftJoin('od.doctor', 'd')
->leftJoin('d.user', 'u');
$qb->where($qb->expr()->in('od.doctor', $qb2->getDQL()));
$qb->andWhere('od.organization IN (:organizations)')
->andWhere('d.active = true')
->andWhere('od.active = true')
->setParameter('organizations', $organizations);
在处理abvoe代码时,我收到以下错误:
[Syntax Error] line 0, col 188: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_FROM, got ','
这是我的DoctorSpecialization实体:
/**
* @ORM\Entity
* @ORM\Table(name="doctors_specializations")
*/
class DoctorSpecialization extends BaseEntity{
/**
* @ORM\ManyToOne(targetEntity="Doctor\Entity\Doctor", inversedBy="docSpecialization")
* @ORM\JoinColumn(name="doctor_id",referencedColumnName="id",nullable=false)
*/
protected $doctor;
/**
* @ORM\ManyToOne(targetEntity="Doctor\Entity\Specialization", inversedBy="docSpecialization")
* @ORM\JoinColumn(name="specialization_id", referencedColumnName="id", nullable=false)
*/
protected $specialization;
public function setDoctor(Doctor $doctor = null)
{
$this->doctor = $doctor;
return $this;
}
public function getDoctor()
{
return $this->doctor;
}
public function setSpecialization(Specialization $specialization = null)
{
$this->specialization = $specialization;
return $this;
}
public function getSpecialization()
{
return $this->specialization;
}
}
答案 0 :(得分:0)
你能尝试一下这个代码吗...你的IMO有错误......
//don't use the same query builder for the two querys
$qb = $this->entityManager->createQueryBuilder();
$qb2 = $this->entityManager->createQueryBuilder();
$qb2->select('dsp.doctor')
->from('Doctor\Entity\DoctorSpecialization', 'dsp')
//You are searching by specializationId so join with especialization
->leftJoin('dsp.specialization', 'esp')
->where('esp.id = :specializationId')
->setParameter('specializationId', $searchBy['specialization']);
$qb->select('od', 'd', 'o', 'u')
->from('Doctor\Entity\OrgDoctor', 'od')
->leftJoin('od.organization', 'o')
->leftJoin('od.doctor', 'd')
->leftJoin('d.user', 'u');
$qb->where($qb->expr()->in('od.doctor', $qb2->getDQL()));
//If organizations is an array of ids => o.id... if is an array of entities your code where rigth here
$qb->andWhere('o.id IN (:organizations)')
->andWhere('d.active = true')
->andWhere('od.active = true')
->setParameter('organizations', $organizations);
祝你好运
注意:您还可以使用@ ORM / Column注释将doctor_id和specialization_id声明为您的实体中的私有/受保护属性,这与已声明的关系不冲突并促进查询,因为你不必加入实体(在这种情况下是especialitation),但我不知道这是非常好的做法
答案 1 :(得分:0)
另一个答案,我昨天在思考这个问题并且你不需要子查询,只需要两个连接并修改查询子句
$qb = $this->entityManager->createQueryBuilder();
$qb->select('od', 'd', 'o', 'u')
->from('Doctor\Entity\OrgDoctor', 'od')
->leftJoin('od.organization', 'o')
->leftJoin('od.doctor', 'd')
->leftJoin('d.doctor_specialization', 'dsp')
->leftJoin('dsp.specialization', 'esp')
->leftJoin('d.user', 'u');
$qb->where('esp.id = :specializationId')
//If organizations is an array of ids => o.id... if is an array of entities your code where rigth here
->andWhere('o.id IN (:organizations)')
->andWhere('d.active = true')
->andWhere('od.active = true')
->setParameter('specializationId', $searchBy['specialization'])
->setParameter('organizations', $organizations);
祝你好运