2012-07-31 60 views
0

我想用manyToMany關係添加2個同一對象的實例。ManyToMany的2個實例用於同一類中的同一實體

class A { 

    /** 

* @ORM\ManyToMany(targetEntity="RelatedEntity", inversedBy="teachers", cascade={"persist", "remove"}) 
* @ORM\JoinTable(name="table1", 
*  joinColumns={@ORM\JoinColumn(name="class_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="related_id", referencedColumnName="id")} 
*  ) 
*/ 
protected $myFirstEntities; 


    /** 

* @ORM\ManyToMany(targetEntity="RelatedEntity", inversedBy="students", cascade={"persist", "remove"}) 
* @ORM\JoinTable(name="table2", 
*  joinColumns={@ORM\JoinColumn(name="class_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="related_id", referencedColumnName="id")} 
*  ) 
*/ 
protected $mySecondEntities; 
} 



class RelatedEntity 
{ 
    /** 
* 
* @ORM\ManyToMany(targetEntity="A", mappedBy="myFirstEntities", cascade={"persist"}) 
*/ 
protected $teachers; 

    /** 
* 
* @ORM\ManyToMany(targetEntity="A", mappedBy="mySecondEntities", cascade={"persist"}) 
*/ 
protected $students; 

} 

當我使用

 php app/console doctrine:generate:entities MyApp 

程序產生添加名爲實體的唯一方法:

public function addRelatedEntity(\MyApp\MyBundle\Entity\RelatedEntity $myFirstEntities) 
{ 
    $this->myFirstEntities[] = $myFirstEntities; 
} 

這樣我就可以「T加mySecondEntities我該怎麼辦。我有可能在手動方法中添加它,但是自動方法是不可能的?

我的問題是我可以使用2個特定類的多個關係到同一個類中嗎?我該怎麼做?

回答

0

學說:生成:應該是用來當你需要生成實體的一個時間單位,它不保證生成的映射將是100%正確的,http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/tools.html#entity-generation

此命令不適用於持續使用。它是一個小幫手 ,並不能很好地支持所有的映射邊緣情況。使用此命令後,您仍然需要在您的實體中放置工作。

你可以有很多映射到多爲同一類,只是做手工

public function addFirstEntity(\MyApp\MyBundle\Entity\RelatedEntity $myFirstEntities) 
{ 
    $this->myFirstEntities[] = $myFirstEntities; 
} 

public function addSecondEntity(\MyApp\MyBundle\Entity\RelatedEntity $mySecondEntities) 
{ 
    $this->mySecondEntities[] = $mySecondEntities; 
} 
相關問題