An order holds items, and a customer changes their mind. (The classes keep their French names, Commande for the order and LigneDeCommande for an order item, as in the French version of the exercise.) The removeItem() method of Commande (the order) is the one make:entity generates:
public function removeItem(LigneDeCommande $item): static { if ($this->items->removeElement($item)) { if ($item->getOrder() === $this) { $item->setOrder(null); } } return $this; }
It removes the item from the collection and cuts the link on the item side. But what should Doctrine do with that item on flush()? By default, it saves what it sees: an item without an order. So it tries UPDATE order_item SET order_id = NULL, and with nullable: false on that column, the database refuses:
NOT NULL constraint failed: order_item.order_id
Without that constraint, the item would stay in the database as an orphan. Neither is what we want: an item removed from its order must disappear.
Your task
At the counter, Ada had ordered a coffee, a croissant and a tea. The /orders preview removes the tea, then shows the items left in the database.
- Make an item removed from its order get deleted from the database.
- Check that a deleted order takes its items with it.
removeItem() and the controller don't change: it is the relation's job to say what happens to orphans.