Skip to content

Commit e0edc58

Browse files
committed
minor #17958 [Doctrine] update some doctrine documentation (MrYamous)
This PR was merged into the 6.2 branch. Discussion ---------- [Doctrine] update some doctrine documentation Minor update for doctrine documentation : - Change code example for Entity generated with MakerBundle to use Doctrine's type constants - Replace ManagerRegistry in Controller with EntityManagerInterface Commits ------- 1d6580b update some doctrine documentation
2 parents d38af44 + 1d6580b commit e0edc58

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

doctrine.rst

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,13 @@ methods:
278278
279279
// src/Entity/Product.php
280280
// ...
281+
+ use Doctrine\DBAL\Types\Types;
281282
282283
class Product
283284
{
284285
// ...
285286
286-
+ #[ORM\Column(type: 'text')]
287+
+ #[ORM\Column(type: Types::TEXT)]
287288
+ private $description;
288289
289290
// getDescription() & setDescription() were also added
@@ -350,17 +351,15 @@ and save it::
350351

351352
// ...
352353
use App\Entity\Product;
353-
use Doctrine\Persistence\ManagerRegistry;
354+
use Doctrine\ORM\EntityManagerInterface;
354355
use Symfony\Component\HttpFoundation\Response;
355356
use Symfony\Component\Routing\Annotation\Route;
356357

357358
class ProductController extends AbstractController
358359
{
359360
#[Route('/product', name: 'create_product')]
360-
public function createProduct(ManagerRegistry $doctrine): Response
361+
public function createProduct(EntityManagerInterface $entityManager): Response
361362
{
362-
$entityManager = $doctrine->getManager();
363-
364363
$product = new Product();
365364
$product->setName('Keyboard');
366365
$product->setPrice(1999);
@@ -394,13 +393,10 @@ Take a look at the previous example in more detail:
394393

395394
.. _doctrine-entity-manager:
396395

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.
404400

405401
* **lines 17-20** In this section, you instantiate and work with the ``$product``
406402
object like any other normal PHP object.
@@ -499,16 +495,17 @@ be able to go to ``/product/1`` to see your new product::
499495
namespace App\Controller;
500496

501497
use App\Entity\Product;
498+
use Doctrine\ORM\EntityManagerInterface;
502499
use Symfony\Component\HttpFoundation\Response;
503500
use Symfony\Component\Routing\Annotation\Route;
504501
// ...
505502

506503
class ProductController extends AbstractController
507504
{
508505
#[Route('/product/{id}', name: 'product_show')]
509-
public function show(ManagerRegistry $doctrine, int $id): Response
506+
public function show(EntityManagerInterface $entityManager, int $id): Response
510507
{
511-
$product = $doctrine->getRepository(Product::class)->find($id);
508+
$product = $entityManager->getRepository(Product::class)->find($id);
512509

513510
if (!$product) {
514511
throw $this->createNotFoundException(
@@ -558,7 +555,7 @@ job is to help you fetch entities of a certain class.
558555

559556
Once you have a repository object, you have many helper methods::
560557

561-
$repository = $doctrine->getRepository(Product::class);
558+
$repository = $entityManager->getRepository(Product::class);
562559

563560
// look for a single Product by its primary key (usually "id")
564561
$product = $repository->find($id);
@@ -826,9 +823,8 @@ with any PHP model::
826823
class ProductController extends AbstractController
827824
{
828825
#[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
830827
{
831-
$entityManager = $doctrine->getManager();
832828
$product = $entityManager->getRepository(Product::class)->find($id);
833829

834830
if (!$product) {
@@ -877,7 +873,7 @@ You've already seen how the repository object allows you to run basic queries
877873
without any work::
878874

879875
// from inside a controller
880-
$repository = $doctrine->getRepository(Product::class);
876+
$repository = $entityManager->getRepository(Product::class);
881877
$product = $repository->find($id);
882878

883879
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::
944940
// from inside a controller
945941
$minPrice = 1000;
946942

947-
$products = $doctrine->getRepository(Product::class)->findAllGreaterThanPrice($minPrice);
943+
$products = $entityManager->getRepository(Product::class)->findAllGreaterThanPrice($minPrice);
948944

949945
// ...
950946

0 commit comments

Comments
 (0)