Doctrine中外键的唯一约束

问题描述:

有没有什么方法通过外键在Doctrine中添加约束?这是我在Symfony 3.3中的一个实体的配置。 学说:方案:验证命令让我喜欢回答“没有名为‘产品’表‘关税’没有列”Doctrine中外键的唯一约束

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 
+0

你可以尝试使用注解来替代,它更清晰和容易。 –

+0

感谢您的回答。那么我如何通过注释做到这一点? – kusrabs

+0

你可以在'columns'数组中使用'product_id'和'timeunit_id'来尝试吗? – goto

您需要引用列的名称(而不是名称学说使用的关系)。默认情况下,学说将后缀的关系人的名字与_id,但你可以配置连接列的确切名称显示在下面的示例配置:

'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' 
+0

非常感谢。我立即做出了你的建议,它起作用。 – kusrabs