У меня есть таблица базы данных, которая выглядит следующим образом:Родителя Ребенок Отношение с единым юридическим лицом в доктрине 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 | +----+--------+--------------------+
Как я могу создать объект доктрины 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() для этого объекта. Как я могу это достичь?
Проверьте [этот пример] (http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#one-to-many-self-referencing). Это может также помочь, если вы уточнили, что «это, похоже, не работает», чтобы дать понять, что не работает. – AlexP