2017-10-11 134 views
2

有沒有什麼方法通過外鍵在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

回答

0

您需要引用列的名稱(而不是名稱學說使用的關係)。默認情況下,學說將後綴的關係人的名字與_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