![](/img/trans.png)
[英]Doctrine 2 DQL - how to select inverse side of unidirectional many-to-many query?
[英]How to query the inverse side of a many to many relationship with Doctrine
我想知道公司生產單位的所有病歷中都包含哪些專業疾病。 實體MedicalRecord與DiseaseTypology有很多關系,如下所示:
/**
* AppBundle\Entity\HealthData\MedicalRecord
*
* @ORM\Table(name="medical_record")
* @ORM\Entity(repositoryClass="MedicalRecordRepository")
* @ORM\HasLifecycleCallbacks
*/
class MedicalRecord
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string $companyProductionUnitId
*
* @ORM\Column(name="company_production_unit_id", type="integer",nullable=true)
*/
protected $companyProductionUnitId;
/**
* @var ArrayCollection $professionalDiseases
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\HealthData\Core\DiseaseTypology")
* @ORM\JoinTable(name="medical_record_professional_disease",
* joinColumns={@ORM\JoinColumn(name="medical_record_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="professional_disease_id", referencedColumnName="id")}
* )
*
*/
protected $professionalDiseases;
在MedicalRecordReposity類中,我創建了以下方法:
public function getProfessionalDiseasesByProductionUnit($productionUnitId)
{
$em = $this->getEntityManager();
$repository = $em->getRepository('AppBundle:MedicalRecord');
return $repository->createQueryBuilder('m')
->select('m.professionalDiseases')
->where('m.companyProductionUnitId = :productionUnitId')
->setParameter('productionUnitId', $productionUnitId)
->getQuery()
->getArrayResult();
}
但是我得到了錯誤:
[語義錯誤]行0,'專業疾病'附近的第9列:錯誤:無效的PathExpression。 必須是StateFieldPathExpression。
如何查詢多對多關系的反面? 謝謝!
我不知道我是否能理解您想要什么,但是這是我的嘗試:
class MedicalRecordRepository extends \Doctrine\ORM\EntityRepository
{
public function getProfessionalDiseasesByProductionUnit($productionUnitId)
{
$qb = $this->createQueryBuilder('m');
$qb
->select('m, pd')
->innerJoin('m.professionalDiseases', 'pd')
->where('m.companyProductionUnitId = :productionUnitId')
->setParameter('productionUnitId', $productionUnitId)
;
return $qb->getQuery()->getArrayResult();
}
}
說明:我認為您需要在MedicalRecord
和DiseaseTypology
之間進行MedicalRecord
, DiseaseTypology
,如果您具有此設置(在您的兩個實體中):
#Entity/MedicalRecord.php
private $companyProductionIUnitId;
/**
* @var \AppBundle\Entity\DiseaseTypology
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\DiseaseTypology", mappedBy="medicalRecords")
*/
private $professionalDiseases;
首先,您必須具有那個mappedBy
選項,以告訴教義關系的反面。
和
# Entity/DiseaseTypology.php
/**
* @var \AppBundle\Entity\MedicalRecord
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\MedicalRecord", inversedBy="professionalDiseases")
*/
private $medicalRecords;
您必須具有inversedBy
選項,才能將學說告訴關系的所有者。
一旦我們弄清楚了,要讓學說做與聯接有關的事情,您只需要告訴它進行聯接的字段即可。 在我的示例中, MedicalRecord
和DiseaseTypology
之間的關系是通過$professionalDiseases
字段建立的。 因此,這將是進行聯接的領域:
->innerJoin('m.professionalDiseases', 'pd') // this professionalDiseases is the $professionalDiseses from MedicalRecord entity
好的,我已經做了所有這些解釋,因為我看到了您是如何查詢的,而且我認為這不是正確的方法。
運行getProfessionalDiseasesByProductionUnit()
方法后的結果如下:
注意:使用getResult()而不是getArrayResult(),因為獲取實體(DiseaseTypology)而不是字段集
這里有2個選項:
使關系MedicalRecord <=> DiseaseTypology雙向 請參閱文檔 。 然后您的方法將看起來非常簡單:
public function getProfessionalDiseasesByProductionUnit($productionUnitId) { $em = $this->getEntityManager(); $repository = $em->getRepository(DiseaseTypology::class); return $repository->createQueryBuilder('dt') ->select('dt') ->join('dt.medicalRecords', 'm') ->where('m.companyProductionUnitId = :productionUnitId') ->setParameter('productionUnitId', $productionUnitId) ->getQuery() ->getResult(); }
保留現有的數據庫結構並在查詢后添加一些邏輯
public function getProfessionalDiseasesByProductionUnit($productionUnitId) { $em = $this->getEntityManager(); $repository = $em->getRepository(MedicalRecord::class); $mediaRecords = $repository->createQueryBuilder('m') ->select('m, dt') //note: with this join all professionalDiseases will be loaded within one query for all MedicalRecords ->join('m.professionalDiseases', 'dt') ->where('m.companyProductionUnitId = :productionUnitId') ->setParameter('productionUnitId', $productionUnitId) ->getQuery() ->getResult(); $professionalDiseases = []; foreach($mediaRecords as $mediaRecord) { foreach($mediaRecord->professionalDiseases as $professionalDisease) { $professionalDiseases[professionalDisease->id] = $professionalDisease; } } return $professionalDiseases; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.