@@ -278,12 +278,13 @@ methods:
278
278
279
279
// src/Entity/Product.php
280
280
// ...
281
+ + use Doctrine\DBAL\Types\Types;
281
282
282
283
class Product
283
284
{
284
285
// ...
285
286
286
- + #[ORM\Column(type: 'text' )]
287
+ + #[ORM\Column(type: Types::TEXT )]
287
288
+ private $description;
288
289
289
290
// getDescription() & setDescription() were also added
@@ -350,17 +351,15 @@ and save it::
350
351
351
352
// ...
352
353
use App\Entity\Product;
353
- use Doctrine\Persistence\ManagerRegistry ;
354
+ use Doctrine\ORM\EntityManagerInterface ;
354
355
use Symfony\Component\HttpFoundation\Response;
355
356
use Symfony\Component\Routing\Annotation\Route;
356
357
357
358
class ProductController extends AbstractController
358
359
{
359
360
#[Route('/product', name: 'create_product')]
360
- public function createProduct(ManagerRegistry $doctrine ): Response
361
+ public function createProduct(EntityManagerInterface $entityManager ): Response
361
362
{
362
- $entityManager = $doctrine->getManager();
363
-
364
363
$product = new Product();
365
364
$product->setName('Keyboard');
366
365
$product->setPrice(1999);
@@ -394,13 +393,10 @@ Take a look at the previous example in more detail:
394
393
395
394
.. _doctrine-entity-manager :
396
395
397
- * **line 13 ** The ``ManagerRegistry $doctrine `` argument tells Symfony to
398
- :ref: `inject the Doctrine service <services-constructor-injection >` into the
399
- controller method.
400
-
401
- * **line 15 ** The ``$doctrine->getManager() `` method gets Doctrine's
402
- *entity manager * object, which is the most important object in Doctrine. It's
403
- responsible for saving objects to, and fetching objects from, the database.
396
+ * **line 13 ** The ``EntityManagerInterface $entityManager `` argument tells Symfony
397
+ to :ref: `inject the Entity Manager service <services-constructor-injection >` into
398
+ the controller method. This object is responsible for saving objects to, and
399
+ fetching objects from, the database.
404
400
405
401
* **lines 17-20 ** In this section, you instantiate and work with the ``$product ``
406
402
object like any other normal PHP object.
@@ -499,16 +495,17 @@ be able to go to ``/product/1`` to see your new product::
499
495
namespace App\Controller;
500
496
501
497
use App\Entity\Product;
498
+ use Doctrine\ORM\EntityManagerInterface;
502
499
use Symfony\Component\HttpFoundation\Response;
503
500
use Symfony\Component\Routing\Annotation\Route;
504
501
// ...
505
502
506
503
class ProductController extends AbstractController
507
504
{
508
505
#[Route('/product/{id}', name: 'product_show')]
509
- public function show(ManagerRegistry $doctrine , int $id): Response
506
+ public function show(EntityManagerInterface $entityManager , int $id): Response
510
507
{
511
- $product = $doctrine ->getRepository(Product::class)->find($id);
508
+ $product = $entityManager ->getRepository(Product::class)->find($id);
512
509
513
510
if (!$product) {
514
511
throw $this->createNotFoundException(
@@ -558,7 +555,7 @@ job is to help you fetch entities of a certain class.
558
555
559
556
Once you have a repository object, you have many helper methods::
560
557
561
- $repository = $doctrine ->getRepository(Product::class);
558
+ $repository = $entityManager ->getRepository(Product::class);
562
559
563
560
// look for a single Product by its primary key (usually "id")
564
561
$product = $repository->find($id);
@@ -826,9 +823,8 @@ with any PHP model::
826
823
class ProductController extends AbstractController
827
824
{
828
825
#[Route('/product/edit/{id}', name: 'product_edit')]
829
- public function update(ManagerRegistry $doctrine , int $id): Response
826
+ public function update(EntityManagerInterface $entityManager , int $id): Response
830
827
{
831
- $entityManager = $doctrine->getManager();
832
828
$product = $entityManager->getRepository(Product::class)->find($id);
833
829
834
830
if (!$product) {
@@ -877,7 +873,7 @@ You've already seen how the repository object allows you to run basic queries
877
873
without any work::
878
874
879
875
// from inside a controller
880
- $repository = $doctrine ->getRepository(Product::class);
876
+ $repository = $entityManager ->getRepository(Product::class);
881
877
$product = $repository->find($id);
882
878
883
879
But what if you need a more complex query? When you generated your entity with
@@ -944,7 +940,7 @@ Now, you can call this method on the repository::
944
940
// from inside a controller
945
941
$minPrice = 1000;
946
942
947
- $products = $doctrine ->getRepository(Product::class)->findAllGreaterThanPrice($minPrice);
943
+ $products = $entityManager ->getRepository(Product::class)->findAllGreaterThanPrice($minPrice);
948
944
949
945
// ...
950
946
0 commit comments