2016-12-24 65 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