微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 在Doctrine 2中与单个实体的父子关系

我有这样的数据库表:

+----+--------+--------------------+
| id | parent | description        |
+----+--------+--------------------+
|  1 | null   | P Cat 1            |
|  2 | 1      | Child 1 of P Cat 1 |
|  3 | 1      | Child 2 of P Cat 1 |
|  4 | null   | P Cat 2            |
|  5 | 4      | Child 1 of P Cat 2 |
|  6 | 4      | Child 2 of P Cat 2 |
+----+--------+--------------------+

如何创建具有这些列的doctrine 2实体,但我需要父列将“id”列作为父列引用.当然,父记录具有空的“父”列值.

我有这么公平

PHP
namespace MyNamespace;
use Doctrine\ORM\Mapping AS ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
 * @ORM\Entity
 * @ORM\Table(name="category")
 **/
class Category
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer",name="id")
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * Creates a parent / child relationship on this entity.
     *
     * @ORM\ManyToOne(targetEntity="MyNamespace\Category",inversedBy="id")
     * @ORM\JoinColumn(name="FK_parent_id",referencedColumnName="id",nullable=true)
     */
    protected $parent = null;

    /**
     * @ORM\Column(type="string",name="description",length=250)
     *
     * @var string
     */
    protected $description;

    /**
     * Gets the Primary key value.
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Sets another category ID as the parent of this category.
     */
    public function setParent(Category $category)
    {
        $this->parent = $category;
    }

    /**
     * Clears the parent id and makes it null.
     */
    public function clearParent()
    {
        $this->parent = null;
    }

    /**
     * Sets the description.
     *
     * @param string $description
     * @return Category
     */
    public function setDescription($description)
    {
        $this->description = $description;
        return $this;
    }

    /**
     * Gets the description value.
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }
}

不用说,这似乎不起作用.问题是:

>当另一个实体作为父项添加时,setParent()方法似乎无法按预期工作.
>我需要在这个实体上使用getChildren()方法.我怎样才能做到这一点?

最佳答案
这应该工作:

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;
    }

}

原文地址:https://www.jb51.cc/mysql/432884.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐