doctrine 是否支持多对多连接表中的复合键?

Does doctrine support composite keys in many-to-many join tables?

我有实体 ChannelPostChannel 有简单整数 idPost 有自己的 idchannel_id.

组合键

我想在这些实体(提及)之间建立多对多关系,这样一个 Post 可能有很多被提及的 Channel 并且 Channel 可能被提及许多 Posts.

问题是 - 如果 Doctrine 支持它,我该如何实现这种关系?

Domain\Telegram\Models\Channel:
  type: entity
  id:
    id:
      type: integer

  fields:
    name:
      type: string
      nullable: true

  oneToMany:
    posts:
      targetEntity: Domain\Telegram\Models\Post
      mappedBy: channel


Domain\Telegram\Models\Post:
  type: entity
  id:
    channel:
      associationKey: true
    id:
      type: integer

  manyToOne:
    channel:
      targetEntity: Domain\Telegram\Models\Channel
      inversedBy: posts

更新与解决方案

@WilliamPerron 的解决方案几乎奏效了。如果在当前状态下使用,doctrine:schema:validate returns 错误:

[Mapping] FAIL - The entity-class 'Domain\Telegram\Models\Channel' mapping is invalid:

The inverse join columns of the many-to-many table 'mentions' have to contain to ALL identifier columns of the target entity 'Domain\Telegram\Models\Post', however 'channel_id' are missing.

inverseJoinColumns 部分应如下所示:

    inverseJoinColumns:
      post_id:
        referencedColumnName: id
      post_channel_id:
        referencedColumnName: channel_id

所以这种关系实际上与简单的多对多关系相同。

是的,这是可能的

您只需指定它与 groups 元素连接的 table,然后指定它映射到的实体。对于单向关系,架构将如下所示:

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

可以找到官方文档 here