我有2个表,cart
和product_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
,但它不起作用。我无法弄清楚这个问题。
任何帮助,非常感谢。
即使将字段类型更改为整数并删除生成器策略,它也不起作用。 – codeit
如果将类型更改为整数并移除生成器策略,则需要自行提供值,否则它将为空。 如果您需要一个自动值,请将生成器策略留在适当位置,并使用id作为整数。 –
这不起作用。我需要映射这些实体来获取由'sku'连接的记录。如果我删除了映射,这些值将被正确插入到“cart”表中。你能举一个例子来详细说明你的答案吗? – codeit