Bruna wants to show the ingredients of each dish: travelers have allergies, and the Fire pepper has already sent a dwarf to the infirmary. Pippin has made a start: the Ingredient entity exists, and he wrote the command that tidies the pantry, app:garnir-les-plats (stock the dishes). All that's left is to connect it all.
A dish contains several ingredients, and an ingredient (Honey, Flour…) goes into several dishes: that is a many-to-many relation. In the database, it is stored in a join table holding one row per dish-ingredient pair.
Your mission
- Link
Plat(dish) andIngredientboth ways:$plat->getIngredients()and$ingredient->getPlats(). - Pippin's command starts from the ingredient (
$ingredient->addPlat($plat)): what it puts away must reach the database. - A dish's ingredients come in alphabetical order (by
nom, their name). - Generate the migration, read it, run it, then run
app:garnir-les-platsin the console. - On a dish page, show its ingredients in a
<ul class="ingredients">list, with one<li>per ingredient.
Who decides?
In a two-way relation, Doctrine looks at only one side to write to the database: the owning side. The other side, the inverse side (the one with mappedBy), is only for navigating. If you change only the inverse side, the PHP objects look up to date, and yet nothing is saved.