반응형
다음과 같은 데이터베이스 테이블이 있습니다.
이러한 열이있는 교리 2 엔터티를 만들려면 어떻게해야하지만 "id"열을 부모로 참조하려면 부모 열이 필요합니다. 물론 상위 레코드에는 null "상위"열 값이 있습니다.
너무 공평 해
<?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;
}
}
말할 필요도없이 이것은 작동하지 않는 것 같습니다. 질문은 다음과 같습니다.
해결 방법
이것은 작동합니다.
<?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://stackoverflow.com/questions/21480955
반응형
'MySql' 카테고리의 다른 글
MySQL 동적 파티셔닝 + CREATE AS on HIVE (0) | 2020.12.11 |
---|---|
MySQL Tomcat에 Pentaho BI 5.0.1을 설치하는 방법 (0) | 2020.12.11 |
MySQL Phpstorm. SSH 터널을 통해 mysql 서버에 액세스 할 수 없음 (외부 호스트에 의해 연결이 닫힘) (0) | 2020.12.11 |
MySQL dql을 사용하여 데이터 테이블에서 고유 값을 얻는 방법은 무엇입니까? (0) | 2020.12.11 |
MySQL 데이터 잘림 : 1 행의 'logo'열에 대한 데이터가 너무 깁니다. (0) | 2020.12.10 |
댓글