如何在 ManytoOne 关系中使用 Docrine2 获取行
How can I fetch rows using Docrine2 in a ManytoOne relationship
我有以下表格,我在其中插入、更新等都没有问题。但是,如何获取这种映射的结果行?
Organizations
-->id
-->name
users
-->id
-->first_name
doctors
-->id
-->user_id
org_doctors
-->id
-->org_id
-->doctor_id
这是我的 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;
}
}
这是我的博士实体:
<?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;
}
}
这就是我获取单个医生结果的方式:
$doctor = $this->entityManager->find('Doctor\Entity\Doctor', (int) $doctorId);
如何从 OrgDoctor 实体中获取行?
这是我尝试使用 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
在组织实体中:
/**
* @ORM\OneToMany(targetEntity="Doctor\Entity\OrgDoctor", mappedBy="organization")
*/
protected $orgDoctor;
鉴于您的实体映射,Doctrine 应该为您的 OrgDoctor 实体提供开箱即用的存储库。该存储库实现了一些方法,供您检索该类型的实体。其中之一是 findBy
,其中 return 个 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]));
最后一个示例与 findOneBy
非常相似,return 一个 OrgDoctor 实体而不是数组:
$this->getEntityManager()->getRepository(OrgDoctor::class)->findOneBy(['doctor' => $doctorId, 'organization' => $organizationId]));
如果您打算遍历结果并访问它们的属性或其他关系,您可能想要更改默认存储库策略并为您的 OrgDoctor 实体定义 custom repository。这将允许您做的是编写自定义查询以通过查询构建器 class 和 DQL 检索您的实体。
为了避免N+1 problems,你想在一个循环之前获取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)
;
我有以下表格,我在其中插入、更新等都没有问题。但是,如何获取这种映射的结果行?
Organizations
-->id
-->name
users
-->id
-->first_name
doctors
-->id
-->user_id
org_doctors
-->id
-->org_id
-->doctor_id
这是我的 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;
}
}
这是我的博士实体:
<?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;
}
}
这就是我获取单个医生结果的方式:
$doctor = $this->entityManager->find('Doctor\Entity\Doctor', (int) $doctorId);
如何从 OrgDoctor 实体中获取行?
这是我尝试使用 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
在组织实体中:
/**
* @ORM\OneToMany(targetEntity="Doctor\Entity\OrgDoctor", mappedBy="organization")
*/
protected $orgDoctor;
鉴于您的实体映射,Doctrine 应该为您的 OrgDoctor 实体提供开箱即用的存储库。该存储库实现了一些方法,供您检索该类型的实体。其中之一是 findBy
,其中 return 个 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]));
最后一个示例与 findOneBy
非常相似,return 一个 OrgDoctor 实体而不是数组:
$this->getEntityManager()->getRepository(OrgDoctor::class)->findOneBy(['doctor' => $doctorId, 'organization' => $organizationId]));
如果您打算遍历结果并访问它们的属性或其他关系,您可能想要更改默认存储库策略并为您的 OrgDoctor 实体定义 custom repository。这将允许您做的是编写自定义查询以通过查询构建器 class 和 DQL 检索您的实体。
为了避免N+1 problems,你想在一个循环之前获取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)
;