2016-08-01 39 views
0

我有一个下表结构查询不能

表:医生

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

这个错误意味着你有一个synthax错误的地方,尝试找出问题将帮助ü。 –

回答

0

你能试试这个代码...国际海事组织你有错误......

//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),但我不知道这是非常好的做法

+0

谢谢@Edu,而我正在尝试,我收到以下错误。 [语义错误] line 0,col 145 near'doctor_id FROM':错误:类Doctor \\ Entity \\ DoctorSpecialization没有字段或名为doctor_id的关联 – user3929758

+0

您可以发布您的实体吗? – Edu

+0

我编辑了我的答案 – Edu

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

好运