我在两个实体之间声明了ManyToMany关系,当我使用doctrine:migrations:diff时,它会尝试两次创建表并抛出异常“名为'incompass.surgerysurgeryside'的表已经存在”
这是我使用过的注释:
Surgery.php
/**
* @ORM\ManyToMany(targetEntity="Incompass\SurgeryBundle\Entity\SurgerySide", inversedBy="surgeries")
* @ORM\JoinTable(name="SurgerySurgerySide",
* joinColumns={@ORM\JoinColumn(name="surgery_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="surgery_side_id", referencedColumnName="id")}
* )
*/
protected $surgery_sides;
SurgerySide.php
/**
* @ORM\ManyToMany(targetEntity="Incompass\SurgeryBundle\Entity\Surgery", inversedBy="surgery_sides")
* @ORM\JoinTable(name="SurgerySurgerySide",
* joinColumns={@ORM\JoinColumn(name="surgery_side_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="surgery_id", referencedColumnName="id")}
* )
*/
protected $surgeries;
(NB这里的答案不回答实际问题Doctrine many-to-many relationship wants to create a table twice when I create a migration)
答案 0 :(得分:1)
尝试删除inversedBy
或使用一次inversedBy
和mappedBy
。
/**
* @ORM\ManyToMany(targetEntity="Incompass\SurgeryBundle\Entity\SurgerySide", inversedBy="surgeries")
*/
protected $surgery_sides;
/**
* @ORM\ManyToMany(targetEntity="Incompass\SurgeryBundle\Entity\Surgery", mappedBy="surgery_sides")
*/
protected $surgeries;
Doctrine猜测如何命名表,通常优先级为<firstEntity>_<secondEntity>
。由于两者都是inversedBy
,因此Doctrine不知道哪个使用第二个,因此创建了两个表。