我有2个表,cart
和product_inventory
由sku
映射。product_inventory
将sku
作为主要密钥而不是id
。
映射如下:
AppBundleEntityCart:
type: entity
table: cart
repositoryClass: AppBundleRepositoryCartRepository
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: { }
AppBundleEntityProductInventory:
type: entity
table: product_inventory
repositoryClass: AppBundleRepositoryProductInventoryRepository
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
插入为空。我已经尝试将主键更改为默认值id
,但它不起作用。我无法弄清楚这个问题。
任何帮助都非常感谢。
映射不正确。这应该是相反的方式。购物车可以有许多产品(Onetomany),并且购物车中可能有许多产品。
AppBundleEntityCart:
type: entity
table: cart
repositoryClass: AppBundleRepositoryCartRepository
oneToMany:
inventory:
targetEntity: ProductInventory
mappedBy: cart
AppBundleEntityProductInventory:
type: entity
table: product_inventory
repositoryClass: AppBundleRepositoryProductInventoryRepository
manyToOne:
cart:
targetEntity: Cart
inversedBy: inventory
joinColumn:
name: sku
referencedColumnName: sku
我感谢大家抽出宝贵的时间来帮助我。
您对sku
字段的定义是字符串,并且将生成器定义为使用Identity Generator策略,该策略取决于使用MySQL,将转换为SKU的AUTO_INCREMENT
列属性。检查策略类型的学说参考。