简体   繁体   中英

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:

<?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?

this is how i tried using 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. 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:

$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:

$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. 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.

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:

$qb->select('od', 'd', 'o')
   ->from(OrgDoctor::class, 'od')
   ->join('od.organization', 'o')
   ->join('od.doctor', 'd')
   ->where('od.doctor = :doctor')
   ->setParameter('doctor', $doctorId)
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM