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個特定類的多個關係到同一個類中嗎?我該怎麼做?