The menu comes out of the database

Exercise 2 of 6 in chapter 2 · 150 XP · 5 objectives checked by tests

Topics covered: RepositoryDependency injectionConsole command

The Plat entity exists, and so does the table. Bruna even wrote a command to fill her menu: app:charger-carte (load the menu). Every time the preview loads, the console runs for you:

bin/console doctrine:schema:update --force
bin/console app:charger-carte

Have a look at the Console tab: the menu is in the database (dbal:run-sql "SELECT nom FROM plat"). Yet the site still displays the old list, hard-coded in the Cuisine service.

Your mission

  1. Make Cuisine read the database: inject the PlatRepository into it and rewrite its three methods with it. The PLATS constant must disappear.
  2. Update the menu (menu/index.html.twig): plats is now a list of Plat objects, and the slug is read from plat.slug.

The controllers don't need to change: they already go through Cuisine. That's the whole point of a service.

Going further

Add a dish in SQL from the console, then reload the menu:

dbal:run-sql "INSERT INTO plat (slug, nom, description, prix, specialite) VALUES ('grog', 'Captain''s Grog', 'Hot and treacherous.', 3.2, 0)"

(In SQL, an apostrophe inside a string is written twice: 'Captain''s Grog'.)