我有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