对Doctrine中外键的唯一约束

时间:2017-10-11 13:58:58

标签: symfony doctrine-orm

有没有办法在Doctrine中通过外键添加约束?这是我在Symfony 3.3中为实体配置的。 doctrine:scheme:validation 命令给出了一个答案,例如“表'关税'上没有名称'产品'的列”

Rg\ApiBundle\Entity\Tariff:
    fields:
        price:
            type: float
            column: price
    manyToOne:
        product:
            targetEntity: Product
            inversedBy: tariffs
        timeunit:
            targetEntity: Timeunit
            inversedBy: tariffs
    uniqueConstraints:
        no_double_tariff_idx:
            columns:
                - product
                - timeunit

1 个答案:

答案 0 :(得分:0)

您需要引用列的名称(而不是doctrine使用的关系的名称)。默认情况下,doctrine会将关系的名称后缀为_id,但您可以配置join-column的确切名称,如以下示例配置所示:

'Your\Entity\ProductVariant':
  manyToOne:
    image:
      targetEntity: 'Your\Entity\Product\Image'
      joinColumn:
        name: '`image_id`'
        referencedColumnname: 'id'
        nullable: false
        options:
          unique: false
    color:
      targetEntity: 'Your\Entity\Product\Color'
      joinColumn:
        name: '`color_id`'
        referencedColumnname: 'id'
        # [..]
  uniqueConstraints:
    only_one_image_of_same_product_color_idx:
      columns:
        - 'image_id'
        - 'color_id'