OneToMany
5 exercises work on this concept, by writing code in the browser.
In the tracks
In Practice
- Never replace a Doctrine collection With orphanRemoval, a setItems() that replaces the collection wipes every item in the database, even the ones passed back to it. An adder keeps them.
- Deleting an item removed from its order Removing an item from the collection doesn't delete it: orphanRemoval tells Doctrine that an item without an order has no reason to exist.
- Saving an order with its items “A new entity was found through the relationship”: with cascade: ['persist'], a single persist() saves the order and its items.
- Keeping both sides of a OneToMany relation in sync Adding an item to an order's collection doesn't save it: Doctrine only reads the owning side, the ManyToOne.