【问题标题】:Compound foreign key for a OneToMany in yaml (symfony2)yaml中OneToMany的复合外键(symfony2)
【发布时间】:2014-07-01 20:07:08
【问题描述】:

我将向您展示的架构中的每个实体都以这种方式定义:[ENTITY]。 每个实体都用“.idName”标识。 “.idName1.IdName2”等多个字段创建一个复合键,其中至少其中一个是外键。

[Device.deviceId] has [Bookable.deviceId.bookableId]
(.deviceId is a identifies a device)

[Bookable.deviceId.bookableId] has [Booking.deviceId.bookableId.bookingId]
(the tuple .deviceId.bookableId identifies a Bookable)

我已经实现了在 [Device] 和 [Bookable] 之间创建 oneToMany 关系(请参阅代码),但是当我想在 Bookable 和 Booking 之间建立另一个 oneToMany 关系时,它不起作用并且 Doctrine 给了我一个错误。运行后:

php app/console doctrine:schema:update

显示此错误:

[Doctrine\ORM\ORMException]                                                                                  
Column name `deviceId` referenced for relation from SearchDevice\WebBundle\Entity\Booking towards SearchDev  
ice\WebBundle\Entity\Bookable does not exist.   

完整的 yaml 代码是下一个: Device.orm.yml 文件:

SearchDevice\WebBundle\Entity\Device:
    type: entity
    id:
        deviceId:
            type: integer
            generator: {strategy: AUTO}
    oneToMany:
        bookables:
            targetEntity: Bookable
            mappedBy: device

可预订文件:

SearchDevice\WebBundle\Entity\Bookable:
    type: entity
    id:
        deviceId:
            associationKey: deviceId
        bookableId:
            type: integer
            generator: {strategy: AUTO}
    manyToOne:
        device:
            targetEntity: Device
            inversedBy: bookables
            joinColumns:
                deviceId:
                    referencedColumnName: deviceId
    oneToMany:
        bookings:
            targetEntity: Booking
            mappedBy: bookable

预订文件:

SearchDevice\WebBundle\Entity\Booking:
    type: entity
    id:
        deviceId:
            associationKey: deviceId
        bookableId:
            associationKey: bookableId
        bookingId:
            type: integer
            generator: {strategy: AUTO}
    manyToOne:
        bookable:
            targetEntity: Bookable
            inversedBy: bookings
            joinColumns:
                deviceId:
                    referencedColumnName: deviceId
                bookableId:
                    referencedColumnName: bookableId

看起来问题出在预订实体中,我说“deviceId”是可预订实体的列。由于它是外键列而不是显式列,因此 Doctrine 在处理这种关系时存在问题。

【问题讨论】:

    标签: php symfony doctrine-orm doctrine yaml


    【解决方案1】:

    你为什么不只为你的 manyToOne 关系使用 bookableId 呢? bookableId 有一个自动递增的 id,所以它显然是唯一的。也许一个简单的 joinColumns on

    bookableId:
                    referencedColumnName: bookableId
    

    在您的 Booking 实体中可能很有效。

    我不确定,因为我通常使用注释。但我不明白如果 bookableId 是唯一的,为什么你必须加入 devideId 才能加入预订。

    【讨论】:

    • 感谢您的回答。最后,我决定删除复合 ID 并使用更简单的模型(如您所说,只有一个 id 列)。这不是原始问题的解决方案,而是一种解决方法。这个想法是使用“弱实体”(en.wikipedia.org/wiki/Weak_entity),但看起来很难或不可能实现。如果有人知道该怎么做,我将不胜感激。
    猜你喜欢
    • 2011-11-26
    • 1970-01-01
    • 2015-07-02
    • 2011-02-06
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多