Parent Child Relationship With A Single Entity In Doctrine 2

假装没事ソ 提交于 2019-12-05 11:28:23

This should work:

<?php

use Doctrine\Common\Collections\ArrayCollection;

/** @ORM\Entity */
class Category {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="id")
     * @ORM\GeneratedValue
     */
    protected $id;

    // ...

    /**
     * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
     */
    protected $children;

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
     * @ORM\JoinColumn(name="parent", referencedColumnName="id")
     */
    protected $parent;

    public function __construct() {
        $this->children = new ArrayCollection();
    }

    // Once you have that, accessing the parent and children should be straight forward 
    // (they will be lazy-loaded in this example as soon as you try to access them). IE:

    public function getParent() {
        return $this->parent;
    }

    public function getChildren() {
        return $this->children;
    }

    // ...

    // always use this to setup a new parent/child relationship
    public function addChild(Category $child) {
       $this->children[] = $child;
       $child->setParent($this);
    }

    public function setParent(Category $parent) {
       $this->parent = $parent;
    }

}

To do this in YAML, the configuration would look something like:

AppBundle\Entity\Category:
    type: entity
    table: category
    repositoryClass: AppBundle\Repository\CategoryRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    oneToMany:
        children:
            targetEntity: Category
            mappedBy: parent
    manyToOne:
        parent:
            targetEntity: Category
            inversedBy: children

Source: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-self-referencing

If you're using XML, the configuration would look like this :

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
    <entity repository-class="AppBundle\Repository\CategoryRepository" name="AppBundle\Entity\Category">
        <id name="id" type="integer" column="id">
            <generator strategy="AUTO"/>
        </id>
        <field name="description" type="string" column="description" length="255"/>
        <many-to-one target-entity="Category" inversed-by="children" field="parent">
            <join-column name="parent" referenced-column-name="id"/>
        </many-to-one>
        <one-to-many target-entity="Category" mapped-by="parent" field="child"/>
    </entity>
</doctrine-mapping>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!