Symfony doctrine manyToOne与复合id的关系

时间:2014-08-04 20:35:49

标签: symfony orm doctrine-orm composite-key

在Symfony2应用程序中,我有两个参与者:俱乐部和会员。

我必须使用具有四个特定字段(版本,距离,区域和ID)的复合主键。这是ORM yml。俱乐部架构已正确生成。

MyBundle\Entity\Club:
    type: entity
    oneToMany:
        members:
            targetEntity: Member
            mappedBy: club
    table: null
    id:
        edition:
            type: string
            length: 4
        distance:
            type: string
            length: 1
        district:
            type: integer
        id:
            type: integer
    fields:
        name:
            type: string
            length: 255
        location:
            type: string
            length: 255
    lifecycleCallbacks: {  }

我现在的问题是链接会员实体。这是我目前的ORM yml:

MyBundle\Entity\Member:
    type: entity
    manyToOne:
        club:
            targetEntity: Club
            inversedBy: members
            joinColumn:
                name: club_id
                referencedColumnName: ??? What if composite PK ???
    table: null
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        lastname:
            type: string
            length: 255
        firstname:
            type: string
            length: 255
        ...

设置为referencedColumnName的内容是什么?如何使用复合FK设置引用?

修改

我发现可以使用注释@joinColumns {@joinColumn ... @ joinColumn ...}设置多个连接列。 yml配置类似的东西?我找不到任何例子。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情,我认为它是正确的YML语法:

manyToOne:
  club:
    targetEntity: Club
    inversedBy: members
    joinColumns:
      - joinColumn:
        name:                 club_id
        referencedColumnName: id
      - joinColumn:
        name:                 club_edition
        referencedColumnName: edition
      - joinColumn:
        name:                 club_distance
        referencedColumnName: distance
      - joinColumn:
        name:                 club_district
        referencedColumnName: district

(但是,为了您的持续理智,我强烈建议尽可能避免使用复合键!)