简体   繁体   English

Yaml中的OneToMany的复合外键(symfony2)

[英]Compound foreign key for a OneToMany in yaml (symfony2)

Each entity in the schema that I am going to show you is defined in this way: [ENTITY]. 我将向您显示的架构中的每个实体都是通过以下方式定义的:[ENTITY]。 Every entity is identified with ".idName". 每个实体都用“ .idName”标识。 Several fields like ".idName1.IdName2" create a compound key, where at least one of them is a foreign key. 多个字段(例如“ .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)

I already achieved to create a oneToMany relation between [Device] and [Bookable] (see code) however when I want to make another oneToMany relation between Bookable and Booking it does not work and Doctrine gives me an error. 我已经实现了在[Device]和[Bookable]之间创建一个oneToMany关系(请参见代码),但是当我想在Bookable和Booking之间建立另一个oneToMany关系时,它不起作用,Doctrine给我一个错误。 After running: 运行后:

php app/console doctrine:schema:update

Shows this error: 显示此错误:

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

The full yaml code is the next one: Device.orm.yml file: 完整的yaml代码是下一个代码:Device.orm.yml文件:

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

Bookable file: 可预订的文件:

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

Booking file: 预订文件:

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

Looks like if the problem is in Booking entity, where I am saying that "deviceId" is a column of Bookable entity. 看起来好像问题出在Booking实体中,我在说“ deviceId”是Bookable实体的一列。 Since it is a foreign key column and not an explicit column then Doctrine has problems doing this relationship. 由于它是外键列而不是显式列,因此Doctrine在执行此关系时遇到问题。

Why don't you only use bookableId for your manyToOne relation ? 为什么不为多对关系仅使用bookableId? bookableId haves an auto-increment id so it is obviously unique. bookableId具有自动递增的id,因此它显然是唯一的。 Maybe a simply joinColumns on 也许只需在joinColumns上

bookableId:
                referencedColumnName: bookableId

Could be efficient in your Booking entity. 在您的预订实体中可能会很有效率。

I am not sure because I am usually using annotations. 我不确定,因为我通常使用注释。 But I don't udnerstand why you have to join on devideId to join on bookings if bookableId is unique. 但是我不理解为什么如果bookableId是唯一的,为什么必须加入devideId才能加入预订。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM