3个表格之间的Symfony Doctrine关系

时间:2017-11-23 15:03:43

标签: php mysql symfony doctrine

我是Symfony的新手。我正在使用版本3.我简化了我的示例。我有3个表,对于这个表,我为每个表创建了一个类。

表"人":id |名称 表"组":id |团队名字 表" PersonGroup":id |人|组

每个人可以分成几个小组,每个小组可以有几个人。在表PersonGroup中,我将人与组连接起来。在我的控制器中,我想要求所选人员在哪个组中。但是在Dump中我只有PersonGroup表中的数据而不是Group中的详细信息(在这个例子中是名称):

PersonGroup {#485 ▼
  -id: 5
  -person: Person {#456 ▼
    -id: 4
    -name: "Adam"
    -personGroups: PersistentCollection {#445 ▶}
  }
  -group: Group {#486 ▼
    +__isInitialized__: false
    -id: 5
    -name: null
    -personGroups: null
     …2
  }
}

我的控制器:

/**
 * @Route("/person/{personId}", name="personController")
 */
public function showAction($personId)
{
    $em = $this->getDoctrine()->getManager();
    $item = $em->getRepository('AppBundle:Person')
        ->findOneBy(['id' => $personId]);

    foreach ($person->getPersonGroup() as $personGroup) {
        dump($personGroup);
    }

    return $this->render('person/detail.html.twig', [
        'person' => $person
    ]);
}

人类/实体:

class Person
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\PersonGroup", mappedBy="person")
     */
    private $personGroups;

    /**
     * Item constructor.
     */
    public function __construct()
    {
        $this->personGroups= new ArrayCollection();
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

     /**
     * @return ArrayCollection*
     */
    public function getPersonGroups()
    {
        return $this->personGroups;
    }
}

小组类/实体:

class Group
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;

        /**
         * @ORM\OneToMany(targetEntity="AppBundle\Entity\PersonGroup", mappedBy="group")
         */
        private $personGroups;

    /**
     * Item constructor.
     */
    public function __construct()
    {
        $this->personGroups= new ArrayCollection();
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

     /**
     * @return ArrayCollection*
     */
    public function getPersonGroups()
    {
        return $this->personGroups;
    }
}

PersonGroup类/实体:

class PersonGroup 
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Person", inversedBy="group")
     * @ORM\JoinColumn(nullable=false)
     */
    private $person;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Group",  inversedBy="person")
     * @ORM\JoinColumn(nullable=false)
     */
    private $group;

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return Person
     */
    public function getPerson()
    {
        return $this->person;
    }

    /**
     * @param Person $person
     */
    public function setPerson(Person $person)
    {
        $this->person = $person;
    }

    /**
     * @return Group
     */
    public function getGroup()
    {
        return $this->group;
    }

    /**
     * @param Group $group
     */
    public function setGroup(Group $group)
    {
        $this->group = $group;
    }
}

1 个答案:

答案 0 :(得分:2)

您可以从PersonGroup访问您的Group实体

/**
 * @Route("/person/{personId}", name="personController")
 */
public function showAction($personId)
{
    $em = $this->getDoctrine()->getManager();
    $item = $em->getRepository('AppBundle:Person')
        ->findOneBy(['id' => $personId]);

    foreach ($person->getPersonGroup() as $personGroup) {
        dump($personGroup->getGroup());
    }

    return $this->render('person/detail.html.twig', [
        'person' => $person
    ]);
}

但是你应该在Group repository和Person repository中创建一个自定义函数来检索正确的实体。

修改

  1. 如果你想要检索所有的Group实体,你应该在你的关系中添加一个参数fetch =“EAGER”,它会自动对你的关系做一个内部连接。
  2. class PersonGroup 
    {
        /**
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         * @ORM\Column(type="integer")
         */
        private $id;
    
        /**
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Person", inversedBy="group", fetch="EAGER")
         * @ORM\JoinColumn(nullable=false)
         */
        private $person;
    
        /**
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Group",  inversedBy="person", fetch="EAGER")
         * @ORM\JoinColumn(nullable=false)
         */
        private $group;
    
    1. 例如,如果您想从Person获取所有组,您可以执行自定义存储库功能。
    2. 前:

      <?php
      
      namespace AppBundle\Repository;
      
      use AppBundle\Entity\Person;
      
      /**
       * GroupRepository
       *
       * This class was generated by the Doctrine ORM. Add your own custom
       * repository methods below.
       */
      class GroupRepository extends \Doctrine\ORM\EntityRepository
      {
          public function findByPerson(Person $person){
              return $this->createQueryBuilder('g')
                  ->leftJoin('g.personGroups', 'pg')
                  ->leftJoin('pg.person', 'p')
                  ->where('p.id = :personId')
                  ->setParameter('personId', $person->getId())
                  ->getQuery()->getResult();
      
          }
      }