I have a many-to-many relationship defined in my Symfony (using doctrine) project between Orders and Upgrades (an Order can be associated with zero or more Upgrades, and an Upgrade can apply to zero or more Orders).
# schema.yml
Order:
columns:
order_id: {...}
relations:
Upgrades:
class: Upgrade
local: order_id
foreign: upgrade_id
refClass: OrderUpgrade
Upgrade:
columns:
upgrade_id: {...}
relations:
Orders:
class: Order
local: upgrade_id
foreign: order_id
refClass: OrderUpgrade
OrderUpgrade:
columns:
order_id: {...}
upgrade_id: {...}
I want to set up delete cascade behavior so that if I delete an Order or an Upgrade, all of the related OrderUpgrades are deleted. Where do I put onDelete: CASCADE? Usually I would put it at the end of the relations section, but that would seem to imply in this case that deleting Orders would cascade to delete Upgrades. Is Symfony + Doctrine smart enough to know what I’m wanting if I put onDelete: CASCADE in the above relations sections of schema.yml?
After much trial and error, the only way I was able to get it to work was to follow the suggestion in Jestep’s comment and move the relationship definitions including onDelete: CASCADE to the linking table, so in the end it looks like this and behaves how I want it to (deletes cascade from Order to OrderUpgrade, and from Upgrade to OrderUpgrade):
I must say I’m got a bit overwhelmed with all of the different Doctrine Many-to-Many YML examples out there on the internet, each with the relations in slightly different places. A frustrating experience.