Php 一对多:1个实体有2个相关实体

Php 一对多:1个实体有2个相关实体,php,symfony,doctrine-orm,doctrine,Php,Symfony,Doctrine Orm,Doctrine,我试图建立一个一对多的关系,如下所示 我有一个UserProfile实体,它有一些私有字段,它有两个字段是另一个实体: 音乐品味:身份证和姓名 收藏夹艺术家:Id和姓名 起初,我只做了一个onetoMany关系,因为我希望一个用户配置文件有一系列的音乐品味和另一系列的艺术家 这些就是我现在拥有的实体: 用户配置文件: myDomain\Entity\UserProfile: type: entity table: null repositoryClass: MyMel

我试图建立一个一对多的关系,如下所示

我有一个UserProfile实体,它有一些私有字段,它有两个字段是另一个实体:

  • 音乐品味:身份证和姓名
  • 收藏夹艺术家:Id和姓名
起初,我只做了一个onetoMany关系,因为我希望一个用户配置文件有一系列的音乐品味和另一系列的艺术家

这些就是我现在拥有的实体:

用户配置文件

myDomain\Entity\UserProfile:
    type: entity
    table: null
    repositoryClass: MyMelomanBundle\Repository\UserProfileRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        aboutMe:
            type: string
            length: 255
            nullable: true
            column: about_me
        image:
            type: string
            length: 255
            nullable: true
        birthDate:
            type: datetime
            column: birth_date
            nullable: true
    oneToMany:
        musicalTaste:
            targetEntity: myDomain\Entity\MusicalTaste
            mappedBy: musicalTaste
            joinColumn:
                name: musicalTaste_id
                referencedColumnName: id
        favouriteArtist:
            targetEntity: myDomain\Entity\FavouriteArtist
            mappedBy: favouriteArtist
            joinColumn:
                name: favouriteArtist_id
                referencedColumnName: id

    lifecycleCallbacks: {  }
myDomain\Entity\MusicalTaste:
    type: entity
    table: musical_taste
    repositoryClass: MyMelomanBundle\Repository\MusicalTasteRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
            nullable: true
            column: name
    manyToOne:
        userProfile:
            targetEntity: myDomain\Entity\UserProfile
            inversedBy: musicalTaste
            joinColumn:
                name: userProfile_id
                referencedColumName: id
    lifecycleCallbacks: {  }
myDomain\Entity\FavouriteArtist:
    type: entity
    table: favourite_artist
    repositoryClass: MyMelomanBundle\Repository\FavouriteArtistRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
            nullable: true
            column: name
    manyToOne:
        userProfile:
            targetEntity: myDomain\Entity\UserProfile
            inversedBy: favouriteArtist
            joinColumn:
                name: userProfile_id
                referencedColumName: id
音乐品味

myDomain\Entity\UserProfile:
    type: entity
    table: null
    repositoryClass: MyMelomanBundle\Repository\UserProfileRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        aboutMe:
            type: string
            length: 255
            nullable: true
            column: about_me
        image:
            type: string
            length: 255
            nullable: true
        birthDate:
            type: datetime
            column: birth_date
            nullable: true
    oneToMany:
        musicalTaste:
            targetEntity: myDomain\Entity\MusicalTaste
            mappedBy: musicalTaste
            joinColumn:
                name: musicalTaste_id
                referencedColumnName: id
        favouriteArtist:
            targetEntity: myDomain\Entity\FavouriteArtist
            mappedBy: favouriteArtist
            joinColumn:
                name: favouriteArtist_id
                referencedColumnName: id

    lifecycleCallbacks: {  }
myDomain\Entity\MusicalTaste:
    type: entity
    table: musical_taste
    repositoryClass: MyMelomanBundle\Repository\MusicalTasteRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
            nullable: true
            column: name
    manyToOne:
        userProfile:
            targetEntity: myDomain\Entity\UserProfile
            inversedBy: musicalTaste
            joinColumn:
                name: userProfile_id
                referencedColumName: id
    lifecycleCallbacks: {  }
myDomain\Entity\FavouriteArtist:
    type: entity
    table: favourite_artist
    repositoryClass: MyMelomanBundle\Repository\FavouriteArtistRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
            nullable: true
            column: name
    manyToOne:
        userProfile:
            targetEntity: myDomain\Entity\UserProfile
            inversedBy: favouriteArtist
            joinColumn:
                name: userProfile_id
                referencedColumName: id
喜爱的艺术家

myDomain\Entity\UserProfile:
    type: entity
    table: null
    repositoryClass: MyMelomanBundle\Repository\UserProfileRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        aboutMe:
            type: string
            length: 255
            nullable: true
            column: about_me
        image:
            type: string
            length: 255
            nullable: true
        birthDate:
            type: datetime
            column: birth_date
            nullable: true
    oneToMany:
        musicalTaste:
            targetEntity: myDomain\Entity\MusicalTaste
            mappedBy: musicalTaste
            joinColumn:
                name: musicalTaste_id
                referencedColumnName: id
        favouriteArtist:
            targetEntity: myDomain\Entity\FavouriteArtist
            mappedBy: favouriteArtist
            joinColumn:
                name: favouriteArtist_id
                referencedColumnName: id

    lifecycleCallbacks: {  }
myDomain\Entity\MusicalTaste:
    type: entity
    table: musical_taste
    repositoryClass: MyMelomanBundle\Repository\MusicalTasteRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
            nullable: true
            column: name
    manyToOne:
        userProfile:
            targetEntity: myDomain\Entity\UserProfile
            inversedBy: musicalTaste
            joinColumn:
                name: userProfile_id
                referencedColumName: id
    lifecycleCallbacks: {  }
myDomain\Entity\FavouriteArtist:
    type: entity
    table: favourite_artist
    repositoryClass: MyMelomanBundle\Repository\FavouriteArtistRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
            nullable: true
            column: name
    manyToOne:
        userProfile:
            targetEntity: myDomain\Entity\UserProfile
            inversedBy: favouriteArtist
            joinColumn:
                name: userProfile_id
                referencedColumName: id
当我更新条令模式时,一切看起来都很好,但如果我这样做:

我必须在音乐品味和喜爱的艺术家之间建立一种多人的关系?(之前我做过,但没有创建任何关系…)但验证时有一些失败:

$php bin/控制台原则:模式:验证

[Mapping]  FAIL - The entity-class 'myDomain\Entity\FavouriteArtist' mapping is invalid:
* The mappings myDomain\Entity\FavouriteArtist#userProfile and myDomain\Entity\UserProfile#favouriteArtist are inconsistent with each other.

[Mapping]  FAIL - The entity-class 'myDomain\Entity\MusicalTaste' mapping is invalid:
* The mappings myDomain\Entity\MusicalTaste#userProfile and myDomain\Entity\UserProfile#musicalTaste are inconsistent with each other.

[Mapping]  FAIL - The entity-class 'myDomain\Entity\UserProfile' mapping is invalid:
* The association myDomain\Entity\UserProfile#musicalTaste refers to the owning side field myDomain\Entity\MusicalTaste#musicalTaste which does not exist.
* The association myDomain\Entity\UserProfile#favouriteArtist refers to the owning side field myDomain\Entity\FavouriteArtist#favouriteArtist which does not exist
我怎样才能修好它们?提前感谢

正如梅米特所说:


将userProfile.yml中的“mappedBy:musicalTaste”更改为“mappedBy:userProfile”效果很好。

您可以尝试将userProfile中的“mappedBy:musicalTaste”更改为“mappedBy:userProfile”。yml在验证时通过userProfile更改它们解决了问题,但为什么?musicalTaste是userProfile实体的一个字段。。。无论如何谢谢你!我不知道太多细节。我认为这是教义的YML语法,当它想使用双向方式的桥梁时。你可以阅读条令网站了解详情。