我有看起来像这样的数据库表:
----- --------- ---------------------------- |id |父|描述| ----- --------- ---------------------------- |1 |null |P Cat 1 ||2 |1 |P Cat 1的儿童1 ||3 |1 |P Cat 1的儿童2 ||4 |null |P Cat 2 ||5 |4 |P Cat 2的儿童1 ||6 |4 |P Cat 2的儿童2 | ----- --------- ----------------------------
如何创建一个具有这些列的学说2实体,但是我需要父列将" ID"列作为父。当然,父记录的"父"列值。
公平我有
<?php
namespace MyNamespace;
use DoctrineORMMapping AS ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity
* @ORMTable(name="category")
**/
class Category
{
/**
* @ORMId
* @ORMColumn(type="integer", name="id")
* @ORMGeneratedValue
*/
protected $id;
/**
* Creates a parent / child relationship on this entity.
*
* @ORMManyToOne(targetEntity="MyNamespaceCategory",inversedBy="id")
* @ORMJoinColumn(name="FK_parent_id", referencedColumnName="id", nullable=true)
*/
protected $parent = null;
/**
* @ORMColumn(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 DoctrineCommonCollectionsArrayCollection;
/** @ORMEntity */
class Category {
/**
* @ORMId
* @ORMColumn(type="integer", name="id")
* @ORMGeneratedValue
*/
protected $id;
// ...
/**
* @ORMOneToMany(targetEntity="Category", mappedBy="parent")
*/
protected $children;
/**
* @ORMManyToOne(targetEntity="Category", inversedBy="children")
* @ORMJoinColumn(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;
}
}
在yaml中执行此操作,配置看起来像:
AppBundleEntityCategory:
type: entity
table: category
repositoryClass: AppBundleRepositoryCategoryRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
oneToMany:
children:
targetEntity: Category
mappedBy: parent
manyToOne:
parent:
targetEntity: Category
inversedBy: children
来源:http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/Reference/Association-mmapping.html#one-to-one-to-many-sell-sell-sell-sell-self-refercencing/Div>
如果您使用的是XML,则配置看起来像这样:
<?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="AppBundleRepositoryCategoryRepository" name="AppBundleEntityCategory">
<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>