Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 条令支持多对多联接表中的复合键吗?_Php_Symfony_Doctrine Orm_Composite Primary Key - Fatal编程技术网

Php 条令支持多对多联接表中的复合键吗?

Php 条令支持多对多联接表中的复合键吗?,php,symfony,doctrine-orm,composite-primary-key,Php,Symfony,Doctrine Orm,Composite Primary Key,我有实体Channel和Post频道具有简单整数idPost具有由自己的id和channel\u id组成的复合键 我希望这些实体之间有多对多的关系(提及),这样一个Post可能会有多个Channels被提及,而Channel可能会被许多Posts提及 问题是——如果它得到理论的支持,我如何实现这种关系 Domain\Telegram\Models\Channel: type: entity id: id: type: integer fields: n

我有实体
Channel
Post
<代码>频道具有简单整数
id
Post
具有由自己的
id
channel\u id
组成的复合键

我希望这些实体之间有多对多的关系(提及),这样一个
Post
可能会有多个
Channel
s被提及,而
Channel
可能会被许多
Posts
提及

问题是——如果它得到理论的支持,我如何实现这种关系

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的解决方案几乎奏效了。如果在当前状态下使用,
原则:模式:验证
返回错误:

[映射]失败-实体类“域\电报\模型\通道”映射无效:

多对多表“提及”的反向联接列必须包含目标实体的所有标识符列 “域\电报\模型\邮政”,但缺少“频道id”

inverseJoinColumns部分应如下所示:

    inverseJoinColumns:
      post_id:
        referencedColumnName: id
      post_channel_id:
        referencedColumnName: channel_id
Channel:
  type: entity
  manyToMany:
    posts:
      targetEntity: Post
      joinTable:
        name: posts_channels
        joinColumns:
          channel_id:
            referencedColumnName: id
        inverseJoinColumns:
          post:
            referencedColumnName: id
因此,这种关系实际上与简单的多对多关系相同。

是的,这是可能的 您只需指定它与
groups
元素连接到哪个表,然后指定它映射到的实体。对于单向关系,模式如下所示:

    inverseJoinColumns:
      post_id:
        referencedColumnName: id
      post_channel_id:
        referencedColumnName: channel_id
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

有关此问题的官方文档可以找到。

您确定您的答案适用于使用复合密钥的情况吗?如果您的代码没有显示出对它的任何意识。@svgrafov Doctrine的多对多关系的主要特点是它将负责正确映射ID,并返回
ArrayCollection
中的所有相关记录,因此根本不需要使用复合键。您可以更新代码吗(特别是,
joinColumns
part)更接近我的情况?