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

时间:2017-05-22 21:38:32

标签: php symfony doctrine-orm doctrine

我试图按照以下方式建立一对多关系。

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

  • MusicalTaste:Id和name
  • FavouriteArtist:Id和name。

最初,我只做了一个onetoMany关系,因为想要一个userProfile有一个音乐品味数组和另一个艺术家阵列。

嗯,那些是我现在拥有的实体:

用户配置

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: {  }

MusicalTaste

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: {  }

FavouriteArtists

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

当我更新学说模式时,所有看起来都很好,但是如果我这样做的话:

我必须在MusicalTaste和Favorite Artists上创建manyToOne关系? (我曾经做过,但没有创建任何关系...)但是在验证时有一些失败:

$ php bin / console doctrine:schema:validate

[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

我如何修复它们?提前致谢

1 个答案:

答案 0 :(得分:1)

正如穆罕默德所说:

改变" mappedBy:musicalTaste" as" mappedBy:userProfile"在userprofile.yml工作得很好。