我想添加两个具有manyToMany关系的同一对象实例。
class A {
/**
* @ORMManyToMany(targetEntity="RelatedEntity", inversedBy="teachers", cascade={"persist", "remove"})
* @ORMJoinTable(name="table1",
* joinColumns={@ORMJoinColumn(name="class_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="related_id", referencedColumnName="id")}
* )
*/
protected $myFirstEntities;
/**
* @ORMManyToMany(targetEntity="RelatedEntity", inversedBy="students", cascade={"persist", "remove"})
* @ORMJoinTable(name="table2",
* joinColumns={@ORMJoinColumn(name="class_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="related_id", referencedColumnName="id")}
* )
*/
protected $mySecondEntities;
}
class RelatedEntity
{
/**
*
* @ORMManyToMany(targetEntity="A", mappedBy="myFirstEntities", cascade={"persist"})
*/
protected $teachers;
/**
*
* @ORMManyToMany(targetEntity="A", mappedBy="mySecondEntities", cascade={"persist"})
*/
protected $students;
}
当我使用时
php app/console doctrine:generate:entities MyApp
程序生成一个唯一的方法来添加名为的实体
public function addRelatedEntity(MyAppMyBundleEntityRelatedEntity $myFirstEntities)
{
$this->myFirstEntities[] = $myFirstEntities;
}
所以我不能添加mySecondEntity。我该怎么做。我有可能以手动方式添加它,但自动方式不可能?
我的问题是,我能把一个特定类的2个多对多关系用在同一个类中吗?我该怎么做?
条令:生成:当你需要生成实体时,应该使用一次实体,它不能保证生成的映射是100%正确的,http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/tools.html#entity-生成:
此命令不适合经常使用。它是一个小帮手并且并不能很好地支持所有的映射边缘情况。你仍然使用此命令后,必须将工作放入实体中。
您可以为同一类进行多对多映射,只需手动即可
public function addFirstEntity(MyAppMyBundleEntityRelatedEntity $myFirstEntities)
{
$this->myFirstEntities[] = $myFirstEntities;
}
public function addSecondEntity(MyAppMyBundleEntityRelatedEntity $mySecondEntities)
{
$this->mySecondEntities[] = $mySecondEntities;
}