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

时间:2014-05-13 23:34:24

标签: php symfony doctrine-orm doctrine yaml

我将向您展示的架构中的每个实体都以这种方式定义:[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之间建立另一个onToMany关系时它不起作用而且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"是Bookable实体的一列。由于它是外键列而不是显式列,因此Doctrine在执行此关系时遇到问题。

1 个答案:

答案 0 :(得分:0)

为什么不将bookableId用于manyToOne关系? bookableId具有自动增量ID,因此它显然是唯一的。

可能只是一个简单的joinColumns
bookableId:
                referencedColumnName: bookableId

您的预订实体可以高效。

我不确定因为我经常使用注释。但是,如果bookableId是独一无二的话,我不会认为你必须加入devideId加入预订。