[英]How can I fetch rows using Docrine2 in a ManytoOne relationship
I have the following tables, where I have no problem in inserting, updating etc. But, how can I fetch the result rows for this kind of mapping? 我有以下表格,在其中插入,更新等没有问题。但是,如何获取这种映射的结果行呢?
Organizations
-->id
-->name
users
-->id
-->first_name
doctors
-->id
-->user_id
org_doctors
-->id
-->org_id
-->doctor_id
This is my OrgDoctor Entity: 这是我的OrgDoctor实体:
<?php
namespace Doctor\Entity;
use Doctrine\ORM\Mapping as ORM;
use Library\Entity\BaseEntity;
use User\Entity\User;
use Doctor\Entity\Doctor;
use Organization\Entity\Organization;
/**
* @ORM\Entity
* @ORM\Table(name="org_doctors")
*/
class OrgDoctor extends BaseEntity{
/**
* @ORM\ManyToOne(targetEntity="Doctor\Entity\Doctor", inversedBy="orgDoctor")
* @ORM\JoinColumn(name="doctor_id",referencedColumnName="id",nullable=false)
*/
protected $doctor;
/**
* @ORM\ManyToOne(targetEntity="Organization\Entity\Organization", inversedBy="orgDoctor")
* @ORM\JoinColumn(name="org_id", referencedColumnName="id", nullable=false)
*/
protected $organization;
public function setDoctor(Doctor $doctor = null)
{
$this->doctor = $doctor;
return $this;
}
public function getDoctor()
{
return $this->doctor;
}
public function setOrganization(Organization $organization = null)
{
$this->organization = $organization;
return $this;
}
public function getOrganization()
{
return $this->organization;
}
}
And this is my Doctor Entity: 这是我的医生实体:
<?php
namespace Doctor\Entity;
use Library\Entity\BaseEntity;
use Users\Entity\User;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="doctors")
*/
class Doctor extends BaseEntity {
/**
* @ORM\OneToOne(targetEntity="Users\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
* @var Users\Entity\User
*/
private $user;
/**
* @ORM\Column(name="summary", type="string")
* @var string
*/
private $summary;
function getUser() {
return $this->user;
}
function setUser(User $user) {
$this->user = $user;
}
function getSummary() {
return $this->summary;
}
function setSummary($summary) {
$this->summary = $summary;
}
}
This is how I'm fetching the result for a single doctor: 这就是我为一位医生获取结果的方式:
$doctor = $this->entityManager->find('Doctor\Entity\Doctor', (int) $doctorId);
How can I fetch rows from the OrgDoctor entity? 如何从OrgDoctor实体获取行?
this is how i tried using queryBuilder: 这是我尝试使用queryBuilder的方式:
$qb = $this->entityManager->createQueryBuilder();
$qb->select('od', 'd', 'o')
->from('Doctor\Entity\OrgDoctor', 'od')
->join('od.organization', 'o')
->join('od.doctor', 'd')
->where('od.organization = :organization')
->setParameter('organization', $orgId);
$query = $qb->getQuery();
$results = $query->getResult();
var_dump($results);
Notice: Undefined index: orgDoctor in C:\xampp\htdocs\corporate-wellness\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php on line 125
In Organization Entity: 在组织实体中:
/**
* @ORM\OneToMany(targetEntity="Doctor\Entity\OrgDoctor", mappedBy="organization")
*/
protected $orgDoctor;
Given your entity mapping, Doctrine should provide you with an out of the box repository for your OrgDoctor entity. 根据您的实体映射,Doctrine应该为您的OrgDoctor实体提供现成的存储库。 That repository implements a few methods for you to retrieve entities of that type.
该存储库实现了一些方法来检索该类型的实体。 One of them is
findBy
, which return arrays of OrgDoctor entities: 其中之一是
findBy
,它返回OrgDoctor实体的数组:
$this->getEntityManager()->getRepository(OrgDoctor::class)->findBy(['doctor' => $doctorId]));
$this->getEntityManager()->getRepository(OrgDoctor::class)->findBy(['organization' => $organizationId]));
$this->getEntityManager()->getRepository(OrgDoctor::class)->findBy(['doctor' => $doctorId, 'organization' => $organizationId]));
The last example, would be quite similar to findOneBy
, which would return an OrgDoctor entity instead of an array: 最后一个示例与
findOneBy
非常相似,它将返回一个OrgDoctor实体而不是一个数组:
$this->getEntityManager()->getRepository(OrgDoctor::class)->findOneBy(['doctor' => $doctorId, 'organization' => $organizationId]));
If you're planning to loop through the results and access their properties or other relationships, you might want to change the default repository strategy and define a custom repository for your OrgDoctor entity. 如果您打算遍历结果并访问其属性或其他关系,则可能需要更改默认存储库策略并为OrgDoctor实体定义自定义存储库 。 What this will allow you to do, is to write your custom queries for retrieving your entities by means of a query builder class and DQL.
这将允许您做的是编写查询的自定义查询,以通过查询生成器类和DQL检索实体。
In order to avoid N+1 problems , you want to fetch join before a loop to fetch all the necessary associations in one go, so you won't be running N queries within your loop: 为了避免N + 1个问题 ,您希望在循环之前获取join以便一次性获取所有必要的关联,因此您将不会在循环中运行N个查询:
$qb->select('od', 'd', 'o')
->from(OrgDoctor::class, 'od')
->join('od.organization', 'o')
->join('od.doctor', 'd')
->where('od.doctor = :doctor')
->setParameter('doctor', $doctorId)
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.