父母与子女关系与单一实体在教义中2 [英] Parent Child Relationship With A Single Entity In Doctrine 2
本文介绍了父母与子女关系与单一实体在教义中2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库表格如下所示:
I have database table that looks like this:
+----+--------+--------------------+
| 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列作为父项。当然,父记录有一个nullparent列值。
How can I create a doctrine 2 entity that has these columns, but I need the parent column to reference the "id" column as a parent. Of course, a parent record has a null "parent" column value.
所以公平我有
<?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;
}
}
不用说,这似乎不起作用。问题是:
Needless to say, this doesn't appear to work. The questions are:
- 当另一个实体作为父项添加时,setParent()方法看起来不会按预期工作。 li>
- 我需要一个getChildren()方法。如何实现?
推荐答案
这应该是有效的:
<?php
use Doctrine\Common\Collections\ArrayCollection;
/** @ORM\Entity */
class Category {
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
*/
protected $children;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent", referencedColumnName="id")
*/
private $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;
}
}
这篇关于父母与子女关系与单一实体在教义中2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文