@@ -313,14 +313,14 @@ Now you can see this new code in action! Imagine you're inside a controller::
313
313
// ...
314
314
use App\Entity\Category;
315
315
use App\Entity\Product;
316
- use Doctrine\Persistence\ManagerRegistry ;
316
+ use Doctrine\ORM\EntityManagerInterface ;
317
317
use Symfony\Component\HttpFoundation\Response;
318
318
use Symfony\Component\Routing\Annotation\Route;
319
319
320
320
class ProductController extends AbstractController
321
321
{
322
322
#[Route('/product', name: 'product')]
323
- public function index(ManagerRegistry $doctrine ): Response
323
+ public function index(EntityManagerInterface $entityManager ): Response
324
324
{
325
325
$category = new Category();
326
326
$category->setName('Computer Peripherals');
@@ -333,7 +333,6 @@ Now you can see this new code in action! Imagine you're inside a controller::
333
333
// relates this product to the category
334
334
$product->setCategory($category);
335
335
336
- $entityManager = $doctrine->getManager();
337
336
$entityManager->persist($category);
338
337
$entityManager->persist($product);
339
338
$entityManager->flush();
@@ -379,9 +378,9 @@ before. First, fetch a ``$product`` object and then access its related
379
378
380
379
class ProductController extends AbstractController
381
380
{
382
- public function show(ManagerRegistry $doctrine , int $id): Response
381
+ public function show(ProductRepository $productRepository , int $id): Response
383
382
{
384
- $product = $doctrine->getRepository(Product::class) ->find($id);
383
+ $product = $productRepository ->find($id);
385
384
// ...
386
385
387
386
$categoryName = $product->getCategory()->getName();
@@ -412,9 +411,9 @@ direction::
412
411
// ...
413
412
class ProductController extends AbstractController
414
413
{
415
- public function showProducts(ManagerRegistry $doctrine , int $id): Response
414
+ public function showProducts(CategoryRepository $categoryRepository , int $id): Response
416
415
{
417
- $category = $doctrine->getRepository(Category::class) ->find($id);
416
+ $category = $categoryRepository ->find($id);
418
417
419
418
$products = $category->getProducts();
420
419
@@ -433,7 +432,7 @@ by adding JOINs.
433
432
a "proxy" object in place of the true object. Look again at the above
434
433
example::
435
434
436
- $product = $doctrine->getRepository(Product::class) ->find($id);
435
+ $product = $productRepository ->find($id);
437
436
438
437
$category = $product->getCategory();
439
438
@@ -503,9 +502,9 @@ object and its related ``Category`` in one query::
503
502
// ...
504
503
class ProductController extends AbstractController
505
504
{
506
- public function show(ManagerRegistry $doctrine , int $id): Response
505
+ public function show(ProductRepository $productRepository , int $id): Response
507
506
{
508
- $product = $doctrine->getRepository(Product::class) ->findOneByIdJoinedToCategory($id);
507
+ $product = $productRepository ->findOneByIdJoinedToCategory($id);
509
508
510
509
$category = $product->getCategory();
511
510
0 commit comments