如何在ManytoOne关系中使用Docrine2获取行

时间:2016-02-18 14:07:19

标签: php symfony doctrine-orm zend-framework2

我有以下表格,我在插入,更新等方面没有问题。但是,我如何获取这种映射的结果行?

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;

1 个答案:

答案 0 :(得分:1)

鉴于您的实体映射,Doctrine应为您提供OrgDoctor实体的开箱即用存储库。该存储库实现了一些方法来检索该类型的实体。其中一个是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]));

最后一个示例与findOneBy非常相似,它将返回OrgDoctor实体而不是数组:

$this->getEntityManager()->getRepository(OrgDoctor::class)->findOneBy(['doctor' => $doctorId, 'organization' => $organizationId]));

如果您计划循环访问结果并访问其属性或其他关系,则可能需要更改默认存储库策略并为OrgDoctor实体定义custom repository。这将允许您做的是编写自定义查询以通过查询构建器类和DQL检索实体。

为了避免N+1 problems,您希望在循环之前获取联接以一次性获取所有必要的关联,因此您不会在循环中运行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)
;