2016-08-20 87 views
1

我想創建一個FAQ系統,其中系統管理員能在FAQ中的常見問題等,以創建另一個常見問題一SubFAQ ..學說自引用關聯映射(Symfony的)

我知道我需要自我參考,但我怎麼能解決這個問題?

我的實體FAQ.php看起來是這樣的:

/** 
    * @OneToMany(targetEntity="Faq", mappedBy="parent") 
    */ 
private $children; 

/** 
    * @ManyToOne(targetEntity="Faq", inversedBy="children") 
* @JoinColumn(name="parent_id", referencedColumnName="id") 
    */ 
private $parent; 

public function __construct() { 
    $this->children = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

什麼我不明白的是inversedBy以及如何使用這一切。

謝謝。

+0

http://stackoverflow.com/questions/12493865/what-is-the-difference-between-inversedby-and-mappedby#12495834可能會有幫助。 –

+0

也http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html#association-mapping。 –

回答

1

您必須添加一些方法才能添加SubFAQ並返回所有SubFAQ。

/** 
* @param Faq $child 
* 
* @return Faq 
*/ 
public function addSubFAQ($child) 
{ 
    $this->children[] = $child; 

    return $this; 
} 

/** 
* @return ArrayCollection 
*/ 
public function getSubFAQs() 
{ 
    return $this->children; 
}