Reputation: 111
I have 2 tables, cart
and product_inventory
mapped by the column sku
. product_inventory
has sku
as primary key instead of id
.
Mapping is as follows:
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: { }
I'm trying to insert a record into the cart
table but the mapped key sku
is inserted as NULL. I've tried changing the primary key to the default value id
, but it didn't work. I'm not able to figure out the issue.
Any help is greatly appreciated.
Upvotes: 0
Views: 138
Reputation: 111
The mapping was not correct. It should have been the other way around. A cart can have many products (oneToMany) and there can be many products in a cart (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
I thank you all for taking the time to help me out.
Upvotes: 1
Reputation: 6908
Your definition of the sku
field is a string and your generator is defined to use IDENTITY generator strategy which depending if using mysql will be translated to AUTO_INCREMENT
column attribute for the sku. Check the doctrine reference for the strategies types.
Upvotes: 0