2015-10-24 109 views
0

是否有可能在實體中有自我參照領域?像這樣:Symfony主義自我參照

class Dir 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="integer") 
    */ 
    protected $parent; // reference to other Dir 
} 
+0

看看這個[章節](http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-self-在Doctrine的文檔中引用)。 – Artamiel

回答

3

常見關聯和自引用沒有區別。

下可能的工作:

class Dir 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\ManyToOne(targetEntity="Dir") 
    */ 
    private $parent; 
} 

瞭解更多關於協會Doctrinehere

如果您不打算從您的實體繼承,也應該使用private屬性。