我有一个项目,其中同一数据库上有 OneToMany 关系。
目前它的设计是这样的:
/**
* @ORM\OneToMany(targetEntity="MyEntity", mappedBy="myCopiedItem")
*/
protected $mySource;
/**
* @ORM\ManyToOne(targetEntity="MyEntity", inversedBy="mySource")
* @ORM\JoinColumn(name="selected_myentity_copy_id", referencedColumnName="id")
*/
protected $myCopiedItem;
但现在我必须将这种关系设为ManyToMany。所以我这样做了:
/**
* @ORM\ManyToMany(targetEntity="MyEntity", mappedBy="myCopiedItem")
*/
protected $mySource;
/**
* @ORM\ManyToMany(targetEntity="MyEntity", inversedBy="mySource")
* @ORM\JoinTable(name="entity_has_copy")
*/
protected $myCopiedItem;
但是 symfony 创建的“entity_has_copy”表只有 1 个项目(myentity_id),我想要 2 个字段“myentity_id”和“selected_myentity_copy_id”,它们实际上都是我的“myentity”表中的 id...
为了在生成的表中包含两个 id,我必须修改什么?
我确信我错过了一些东西,但我不知道是什么:(
Note: Entity / table names were renamed for privacy
最佳答案
解决了这个问题!
我必须在定义中添加关系...
所以这是 JoinTable 部分的正确定义:
/**
* @ORM\ManyToMany(targetEntity="MyEntity", inversedBy="mySource")
* @ORM\JoinTable(name="entity_has_copy",
* joinColumns={@ORM\JoinColumn(name="entity_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="entity_copy_id", referencedColumnName="id")}
* )
*/
protected $myCopiedItem;
希望这能帮助其他遇到同样问题的人...
如果您想了解有关如何使用 Doctrine 映射实体之间关联的更多信息,请参阅 a good URL !
关于symfony - 同一张表上的多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15114666/