@@ -367,11 +367,9 @@ and save it::
367367 /**
368368 * @Route("/product", name="create_product")
369369 */
370- public function createProduct(): Response
370+ public function createProduct(ManagerRegistry $doctrine ): Response
371371 {
372- // you can fetch the EntityManager via $this->getDoctrine()
373- // or you can add an argument to the action: createProduct(EntityManagerInterface $entityManager)
374- $entityManager = $this->getDoctrine()->getManager();
372+ $entityManager = $doctrine->getManager();
375373
376374 $product = new Product();
377375 $product->setName('Keyboard');
@@ -406,7 +404,11 @@ Take a look at the previous example in more detail:
406404
407405.. _doctrine-entity-manager :
408406
409- * **line 18 ** The ``$this->getDoctrine()->getManager() `` method gets Doctrine's
407+ * **line 14 ** The ``ManagerRegistry $doctrine `` argument tells Symfony to
408+ :ref: `inject the Doctrine service <services-constructor-injection >` into the
409+ controller method.
410+
411+ * **line 16 ** The ``$doctrine->getManager() `` method gets Doctrine's
410412 *entity manager * object, which is the most important object in Doctrine. It's
411413 responsible for saving objects to, and fetching objects from, the database.
412414
@@ -516,11 +518,9 @@ be able to go to ``/product/1`` to see your new product::
516518 /**
517519 * @Route("/product/{id}", name="product_show")
518520 */
519- public function show(int $id): Response
521+ public function show(ManagerRegistry $doctrine, int $id): Response
520522 {
521- $product = $this->getDoctrine()
522- ->getRepository(Product::class)
523- ->find($id);
523+ $product = $doctrine->getRepository(Product::class)->find($id);
524524
525525 if (!$product) {
526526 throw $this->createNotFoundException(
@@ -571,7 +571,7 @@ job is to help you fetch entities of a certain class.
571571
572572Once you have a repository object, you have many helper methods::
573573
574- $repository = $this->getDoctrine() ->getRepository(Product::class);
574+ $repository = $doctrine ->getRepository(Product::class);
575575
576576 // look for a single Product by its primary key (usually "id")
577577 $product = $repository->find($id);
@@ -667,9 +667,9 @@ with any PHP model::
667667 /**
668668 * @Route("/product/edit/{id}")
669669 */
670- public function update(int $id): Response
670+ public function update(ManagerRegistry $doctrine, int $id): Response
671671 {
672- $entityManager = $this->getDoctrine() ->getManager();
672+ $entityManager = $doctrine ->getManager();
673673 $product = $entityManager->getRepository(Product::class)->find($id);
674674
675675 if (!$product) {
@@ -718,8 +718,7 @@ You've already seen how the repository object allows you to run basic queries
718718without any work::
719719
720720 // from inside a controller
721- $repository = $this->getDoctrine()->getRepository(Product::class);
722-
721+ $repository = $doctrine->getRepository(Product::class);
723722 $product = $repository->find($id);
724723
725724But what if you need a more complex query? When you generated your entity with
@@ -786,9 +785,7 @@ Now, you can call this method on the repository::
786785 // from inside a controller
787786 $minPrice = 1000;
788787
789- $products = $this->getDoctrine()
790- ->getRepository(Product::class)
791- ->findAllGreaterThanPrice($minPrice);
788+ $products = $doctrine->getRepository(Product::class)->findAllGreaterThanPrice($minPrice);
792789
793790 // ...
794791
0 commit comments