Php 如何查询多对多关系与信条的反方向
我想知道公司生产单位的所有病历中都包括哪些专业疾病。实体MedicalRecord与疾病类型学有多对多关系,如下所示:Php 如何查询多对多关系与信条的反方向,php,symfony,doctrine,many-to-many,Php,Symfony,Doctrine,Many To Many,我想知道公司生产单位的所有病历中都包括哪些专业疾病。实体MedicalRecord与疾病类型学有多对多关系,如下所示: /** * AppBundle\Entity\HealthData\MedicalRecord * * @ORM\Table(name="medical_record") * @ORM\Entity(repositoryClass="MedicalRecordRepository") * @ORM\HasLifecycleCallbacks */ class Med
/**
* 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列“professionalDiseases”附近:错误:无效的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
之间建立连接,为此,如果您有此设置(在两个实体中):
首先,您必须有mappedBy
选项,以告知关系的反面
及
你必须有inversedBy
选项来告知关系的拥有方
一旦我们澄清了这一点,为了让doctrine完成与连接相关的事情,您只需要告诉它在哪个字段上进行连接。在我的例子中,MedicalRecord
和DiseaseTypology
之间的关系是通过$professionalDiseases
字段建立的。因此,此字段将成为进行连接的字段:
->innerJoin('m.professionalDiseases', 'pd') // this professionalDiseases is the $professionalDiseses from MedicalRecord entity
好的,我已经做了所有这些解释,因为我看到了你是如何进行查询的,我觉得这不是正确的方法
运行getProfessionalDiseasesByProductionUnit()
方法后的结果如下:
我不知道我是否能理解你想要什么,但这是我的尝试:
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
之间建立连接,为此,如果您有此设置(在两个实体中):
首先,您必须有mappedBy
选项,以告知关系的反面
及
你必须有inversedBy
选项来告知关系的拥有方
一旦我们澄清了这一点,为了让doctrine完成与连接相关的事情,您只需要告诉它在哪个字段上进行连接。在我的例子中,MedicalRecord
和DiseaseTypology
之间的关系是通过$professionalDiseases
字段建立的。因此,此字段将成为进行连接的字段:
->innerJoin('m.professionalDiseases', 'pd') // this professionalDiseases is the $professionalDiseses from MedicalRecord entity
好的,我已经做了所有这些解释,因为我看到了你是如何进行查询的,我觉得这不是正确的方法
运行getProfessionalDiseasesByProductionUnit()
方法后的结果如下:
注意:使用getResult()而不是getArrayResult(),因为您获取的是实体(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;
}
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;
}
可能是这些,可以帮助您改进问题或回答问题。可能是这些,可以帮助您改进问题或回答问题。您的建议是可以的,但查询返回一系列与相关专业疾病相关的医疗记录。我希望能直接收到一系列专业疾病,用于特定公司单位的所有医疗记录。可能吗?Thx@Gianluca78如果我很了解你,你需要一个基于特定公司单位的每个医疗记录的职业疾病数组?我需要一个具有特定公司id的医疗记录中确定的所有职业疾病数组。例如:$professionalDiseasesForCompanyUnit=array('disease 1'、'disease 2'、'disease 44'))好吧,我知道该怎么办了!我把模型弄错了。很抱歉再次打扰您…您的建议是可以的,但是查询返回一系列与相关专业疾病相关的医疗记录。我希望能直接收到一系列专业疾病,用于特定公司单位的所有医疗记录。可能吗?Thx@Gianluca78如果我很了解你,你需要一个基于特定公司单位的每个医疗记录的职业疾病数组?我需要一个具有特定公司id的医疗记录中确定的所有职业疾病数组。例如:$professionalDiseasesForCompanyUnit=array('disease 1'、'disease 2'、'disease 44'))好吧,我知道该怎么办了!我把模型弄错了。很抱歉再次打扰你。。。