我有实体Channel
和Post
。Channel
有简单的整数id
。Post
拥有由自己的id
和channel_id
制成的复合键。
我希望在这些实体之间建立多对多关系(提及),以便一个Post
可能会提到许多Channel
,Channel
可能会被许多Posts
提及。
问题是 - 如果它得到教义的支持,我如何实现这种关系?
DomainTelegramModelsChannel:
type: entity
id:
id:
type: integer
fields:
name:
type: string
nullable: true
oneToMany:
posts:
targetEntity: DomainTelegramModelsPost
mappedBy: channel
DomainTelegramModelsPost:
type: entity
id:
channel:
associationKey: true
id:
type: integer
manyToOne:
channel:
targetEntity: DomainTelegramModelsChannel
inversedBy: posts
更新和解决方案
@WilliamPerron的解决方案几乎奏效了。如果在当前状态下使用,doctrine:schema:validate
返回错误:
[映射] 失败 - 实体类"域\电报\模型\通道"映射无效:
多对多表"提及"的反向连接列必须包含目标实体的所有标识符列 "域\电报\模型\邮政",但是缺少"channel_id"。
反转连接列部分应如下所示:
inverseJoinColumns:
post_id:
referencedColumnName: id
post_channel_id:
referencedColumnName: channel_id
所以这种关系实际上与简单的多对多关系相同。
是的,这是可能的
您只需指定它与groups
元素联接到哪个表,然后指定它映射到的实体。对于单一关系,架构如下所示:
Channel:
type: entity
manyToMany:
posts:
targetEntity: Post
joinTable:
name: posts_channels
joinColumns:
channel_id:
referencedColumnName: id
inverseJoinColumns:
post:
referencedColumnName: id
对于双向关系,您需要添加inversedBy
元素:
Channel:
type: entity
manyToMany:
posts:
targetEntity: Post
inversedBy: channels
joinTable:
name: posts_channels
joinColumns:
channel_id:
referencedColumnName: id
inverseJoinColumns:
post_id:
referencedColumnName: id
Post:
type: entity
manyToMany:
channels:
targetEntity: Channel
mappedBy: posts
可以在此处找到官方文档。