我想添加具有 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;
}
所以我无法添加 mySecondEntities。我该怎么办。我可以手动添加它,但自动添加它是不可能的?
我的问题是我可以将特定类的 2 个多对多关系用于同一个类吗?我该怎么做?