2016-12-24 62 views
0

我有2個表,cartproduct_inventory由列sku映射。 product_inventory具有sku作爲主鍵而不是id學說:映射鍵被插入爲NULL

映射如下:

AppBundle\Entity\Cart: 
    type: entity 
    table: cart 
    repositoryClass: AppBundle\Repository\CartRepository 
    manyToOne: 
     variant: 
     targetEntity: ProductInventory 
     inversedBy: cart 
     joinColumn: 
      name: sku 
      referencedColumnName: sku 
    id: 
     id: 
      type: integer 
      nullable: false 
      options: 
       unsigned: true 
      id: true 
      generator: 
       strategy: IDENTITY 
    fields: 
     userId: 
      type: integer 
      nullable: false 
      options: 
       unsigned: false 
      column: user_id 
     sku: 
      type: string 
      nullable: false 
      length: 10 
      options: 
       fixed: false 
     quantity: 
      type: tinyint 
      nullable: false 
    lifecycleCallbacks: { } 

AppBundle\Entity\ProductInventory: 
    type: entity 
    table: product_inventory 
    repositoryClass: AppBundle\Repository\ProductInventoryRepository 
    manyToOne: 
     product: 
      targetEntity: Product 
      inversedBy: inventory 
      joinColumn: 
       name: product_id 
       referencedColumnName: id 
    oneToMany: 
     attribute_value: 
      targetEntity: ProductAttributeValue 
      mappedBy: inventory 
      cascade: [persist] 
     cart: 
      targetEntity: Cart 
      mappedBy: variant 
    id: 
     sku: 
      type: string 
      nullable: false 
      length: 10 
      options: 
       fixed: false 
      id: true 
      generator: 
       strategy: IDENTITY 
    fields: 
     productId: 
      type: integer 
      nullable: false 
      options: 
       unsigned: true 
      column: product_id 
     quantityAvailable: 
      type: tinyint 
      nullable: false 
      column: quantity_available 
     quantitySold: 
      type: smallint 
      nullable: false 
      options: 
       unsigned: true 
      column: quantity_sold 
    lifecycleCallbacks: { } 

我試圖插入一條記錄到cart表,但映射的關鍵sku插入爲NULL。我嘗試將主鍵更改爲默認值id,但它不起作用。我無法弄清楚這個問題。

任何幫助,非常感謝。

回答

1

映射不正確。應該是相反的。一個購物車可以有許多產品(oneToMany),並且購物車中可以有許多產品(manyToOne)。

AppBundle\Entity\Cart: 
    type: entity 
    table: cart 
    repositoryClass: AppBundle\Repository\CartRepository 
    oneToMany: 
     inventory: 
      targetEntity: ProductInventory 
      mappedBy: cart 

AppBundle\Entity\ProductInventory: 
    type: entity 
    table: product_inventory 
    repositoryClass: AppBundle\Repository\ProductInventoryRepository 
    manyToOne: 
     cart: 
      targetEntity: Cart 
      inversedBy: inventory 
      joinColumn: 
      name: sku 
      referencedColumnName: sku 

我感謝大家抽出寶貴時間來幫助我。

0

你的sku領域的定義是一個字符串,您的發電機被定義爲使用的依賴,如果使用MySQL將被轉換爲SKU AUTO_INCREMENT列屬性IDENTITY生成器策略。檢查doctrine reference的策略類型。

+0

即使將字段類型更改爲整數並刪除生成器策略,它也不起作用。 – codeit

+0

如果將類型更改爲整數並移除生成器策略,則需要自行提供值,否則它將爲空。 如果您需要一個自動值,請將生成器策略留在適當位置,並使用id作爲整數。 –

+0

這不起作用。我需要映射這些實體來獲取由'sku'連接的記錄。如果我刪除了映射,這些值將被正確插入到「cart」表中。你能舉一個例子來詳細說明你的答案嗎? – codeit