主义无法映射关联

时间:2019-01-17 20:08:45

标签: symfony doctrine-orm doctrine

情况是:用户与Brother一对一关联,而Brother可以阻止其他Brother。

然后我无法将BrotherHasBrotherBlocked#brother映射为标识符,因为目标实体Brother也将关联映射为标识符,但是我每个实体都必须有一个标识符,并且我不知道如何摆脱这种映射失败。

以下映射的实体:

实体用户:

App\Domain\Model\User\User:
    type: entity
    table: user
    id:
        id:
            type: integer
            nullable: false
            options:
                unsigned: true
            id: true
            generator:
                trategy: IDENTITY
    fields:
        ...
    oneToOne:
        brother:
            targetEntity: App\Domain\Model\User\Brother\Brother
            cascade: ["persist"]
            fetch: LAZY
            mappedBy: user
            joinColumns:
                user_id:
                    referencedColumnName: id
            orphanRemoval: false

实体兄弟

App\Domain\Model\User\Brother\Brother:
    type: entity
    table: brother
    id:
        user:
            associationKey: true
    fields:
        ...
    oneToOne:
        user:
            targetEntity: App\Domain\Model\User\User
            cascade: ["persist"]
            fetch: LAZY
            inversedBy: brother
            joinColumns:
                id:
                    referencedColumnName: id
            orphanRemoval: false
    oneToMany:
        brothersBlocked:
            targetEntity: App\Domain\Model\User\Brother\BrotherHasBrotherBlocked
            mappedBy: brother
            cascade: ["persist"]               

实体BrotherHasBrotherBlocked

App\Domain\Model\User\Brother\BrotherHasBrotherBlocked:
    type: entity
    table: brother_has_brother_blocked
    id:
        brother:
            associationKey: true
        brotherBlocked:
            associationKey: true
    fields:
        ...
    manyToOne:
        brother:
            targetEntity:  App\Domain\Model\User\Brother\Brother
            cascade: ["persist"]
            inversedBy: brothersBlocked
            joinColumns:
                brother_id:
                    referencedColumnName: id
            orphanRemoval: false
        brotherBlocked:
            targetEntity:  App\Domain\Model\User\Brother\Brother
            cascade: ["persist"]
            inversedBy: brothersBlocked
            joinColumns:
                brother_blocked_id:
                    referencedColumnName: id
            orphanRemoval: false
    lifecycleCallbacks: {  }

运行:bin /控制台学说:schema:validate

  

映射

     

[FAIL]实体类   App \ Domain \ Model \ User \ Brother \ BrotherHasBrotherBlocked映射为   无效:   *无法将关联'App \ Domain \ Model \ User \ Brother \ BrotherHasBrotherBlocked#brother关联为   标识符,因为目标实体   'App \ Domain \ Model \ User \ Brother \ Brother'也将关联映射为   标识符。   *无法映射关联'App \ Domain \ Model \ User \ Brother \ BrotherHasBrotherBlocked#brotherBlocked   作为标识符,因为目标实体   'App \ Domain \ Model \ User \ Brother \ Brother'也将关联映射为   标识符。   *映射App \ Domain \ Model \ User \ Brother \ BrotherHasBrotherBlocked#brotherBlocked   和App \ Domain \ Model \ User \ Brother \ Brother#brothersBlocked是   彼此不一致。

我想在关联的关系得到一个整数而不是正确的实体类型之后检查do验证。在我成功地存储了关联关系之后,后来又尝试了相同的请求,并针对我在此处定位的其中一个关联关系,得到了这个意外的类型错误。

1 个答案:

答案 0 :(得分:-1)

问题是兄弟使用mappedBy: brother 但是兄弟俩都禁止使用inversedBy: brothersBlocked

您可以从brotherBlocked中删除inversedBy: brothersBlocked,而不会发生任何错误。

或者,您可以将blockedBy属性添加到您的Brother实体中,并设置为brothersBlocked,并为brothersBlocked添加inversedBy: blockedBy

因此您将拥有: 实体兄弟

App\Domain\Model\User\Brother\Brother:
    type: entity
    table: brother
    id:
        user:
            associationKey: true
    fields:
        ...
    oneToOne:
        user:
            targetEntity: App\Domain\Model\User\User
            cascade: ["persist"]
            fetch: LAZY
            inversedBy: brother
            joinColumns:
                id:
                    referencedColumnName: id
            orphanRemoval: false
    oneToMany:
        brothersBlocked:
            targetEntity: App\Domain\Model\User\Brother\BrotherHasBrotherBlocked
            mappedBy: brotherBlocked
            cascade: ["persist"] 
        blockedBy:
            targetEntity: App\Domain\Model\User\Brother\BrotherHasBrotherBlocked
            mappedBy: brother
            cascade: ["persist"]

实体BrotherHasBrotherBlocked

App\Domain\Model\User\Brother\BrotherHasBrotherBlocked:
    type: entity
    table: brother_has_brother_blocked
    id:
        brother:
            associationKey: true
        brotherBlocked:
            associationKey: true
    fields:
        ...
    manyToOne:
        brother:
            targetEntity:  App\Domain\Model\User\Brother\Brother
            cascade: ["persist"]
            inversedBy: brothersBlocked
            joinColumns:
                brother_id:
                    referencedColumnName: id
            orphanRemoval: false
        brotherBlocked:
            targetEntity:  App\Domain\Model\User\Brother\Brother
            cascade: ["persist"]
            inversedBy: blockedBy
            joinColumns:
                brother_blocked_id:
                    referencedColumnName: id
            orphanRemoval: false
    lifecycleCallbacks: {  }