Skip to content

Commit e316093

Browse files
committed
minor #15607 Deprecate getDoctrine() and dispatchMessage() shortcuts (javiereguiluz)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- Deprecate getDoctrine() and dispatchMessage() shortcuts Fixes #15605. Commits ------- 735a24b Deprecate getDoctrine() and dispatchMessage() shortcuts
2 parents ad48336 + 735a24b commit e316093

File tree

7 files changed

+60
-82
lines changed

7 files changed

+60
-82
lines changed

doctrine.rst

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

572572
Once 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
718718
without 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

725724
But 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

doctrine/associations.rst

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,15 @@ Now you can see this new code in action! Imagine you're inside a controller::
320320
// ...
321321
use App\Entity\Category;
322322
use App\Entity\Product;
323+
use Doctrine\Persistence\ManagerRegistry;
323324
use Symfony\Component\HttpFoundation\Response;
324325

325326
class ProductController extends AbstractController
326327
{
327328
/**
328329
* @Route("/product", name="product")
329330
*/
330-
public function index(): Response
331+
public function index(ManagerRegistry $doctrine): Response
331332
{
332333
$category = new Category();
333334
$category->setName('Computer Peripherals');
@@ -340,7 +341,7 @@ Now you can see this new code in action! Imagine you're inside a controller::
340341
// relates this product to the category
341342
$product->setCategory($category);
342343

343-
$entityManager = $this->getDoctrine()->getManager();
344+
$entityManager = $doctrine->getManager();
344345
$entityManager->persist($category);
345346
$entityManager->persist($product);
346347
$entityManager->flush();
@@ -386,12 +387,9 @@ before. First, fetch a ``$product`` object and then access its related
386387

387388
class ProductController extends AbstractController
388389
{
389-
public function show(int $id): Response
390+
public function show(ManagerRegistry $doctrine, int $id): Response
390391
{
391-
$product = $this->getDoctrine()
392-
->getRepository(Product::class)
393-
->find($id);
394-
392+
$product = $doctrine->getRepository(Product::class)->find($id);
395393
// ...
396394

397395
$categoryName = $product->getCategory()->getName();
@@ -422,11 +420,9 @@ direction::
422420
// ...
423421
class ProductController extends AbstractController
424422
{
425-
public function showProducts(int $id): Response
423+
public function showProducts(ManagerRegistry $doctrine, int $id): Response
426424
{
427-
$category = $this->getDoctrine()
428-
->getRepository(Category::class)
429-
->find($id);
425+
$category = $doctrine->getRepository(Category::class)->find($id);
430426

431427
$products = $category->getProducts();
432428

@@ -445,9 +441,7 @@ by adding JOINs.
445441
a "proxy" object in place of the true object. Look again at the above
446442
example::
447443

448-
$product = $this->getDoctrine()
449-
->getRepository(Product::class)
450-
->find($id);
444+
$product = $doctrine->getRepository(Product::class)->find($id);
451445

452446
$category = $product->getCategory();
453447

@@ -517,11 +511,9 @@ object and its related ``Category`` in one query::
517511
// ...
518512
class ProductController extends AbstractController
519513
{
520-
public function show(int $id): Response
514+
public function show(ManagerRegistry $doctrine, int $id): Response
521515
{
522-
$product = $this->getDoctrine()
523-
->getRepository(Product::class)
524-
->findOneByIdJoinedToCategory($id);
516+
$product = $doctrine->getRepository(Product::class)->findOneByIdJoinedToCategory($id);
525517

526518
$category = $product->getCategory();
527519

doctrine/multiple_entity_managers.rst

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,18 @@ the default entity manager (i.e. ``default``) is returned::
232232

233233
// ...
234234
use Doctrine\ORM\EntityManagerInterface;
235+
use Doctrine\Persistence\ManagerRegistry;
235236

236237
class UserController extends AbstractController
237238
{
238-
public function index(EntityManagerInterface $entityManager): Response
239+
public function index(ManagerRegistry $doctrine): Response
239240
{
240-
// These methods also return the default entity manager, but it's preferred
241-
// to get it by injecting EntityManagerInterface in the action method
242-
$entityManager = $this->getDoctrine()->getManager();
243-
$entityManager = $this->getDoctrine()->getManager('default');
244-
$entityManager = $this->get('doctrine.orm.default_entity_manager');
241+
// Both methods return the default entity manager
242+
$entityManager = $doctrine->getManager();
243+
$entityManager = $doctrine->getManager('default');
245244

246-
// Both of these return the "customer" entity manager
247-
$customerEntityManager = $this->getDoctrine()->getManager('customer');
248-
$customerEntityManager = $this->get('doctrine.orm.customer_entity_manager');
245+
// This method returns instead the "customer" entity manager
246+
$customerEntityManager = $doctrine->getManager('customer');
249247

250248
// ...
251249
}
@@ -267,29 +265,21 @@ The same applies to repository calls::
267265

268266
use AcmeStoreBundle\Entity\Customer;
269267
use AcmeStoreBundle\Entity\Product;
268+
use Doctrine\Persistence\ManagerRegistry;
270269
// ...
271270

272271
class UserController extends AbstractController
273272
{
274-
public function index(): Response
273+
public function index(ManagerRegistry $doctrine): Response
275274
{
276-
// Retrieves a repository managed by the "default" em
277-
$products = $this->getDoctrine()
278-
->getRepository(Product::class)
279-
->findAll()
280-
;
275+
// Retrieves a repository managed by the "default" entity manager
276+
$products = $doctrine->getRepository(Product::class)->findAll();
281277

282-
// Explicit way to deal with the "default" em
283-
$products = $this->getDoctrine()
284-
->getRepository(Product::class, 'default')
285-
->findAll()
286-
;
278+
// Explicit way to deal with the "default" entity manager
279+
$products = $doctrine->getRepository(Product::class, 'default')->findAll();
287280

288-
// Retrieves a repository managed by the "customer" em
289-
$customers = $this->getDoctrine()
290-
->getRepository(Customer::class, 'customer')
291-
->findAll()
292-
;
281+
// Retrieves a repository managed by the "customer" entity manager
282+
$customers = $doctrine->getRepository(Customer::class, 'customer')->findAll();
293283

294284
// ...
295285
}

forms.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,6 @@ written into the form object::
397397
$task = $form->getData();
398398

399399
// ... perform some action, such as saving the task to the database
400-
// for example, if Task is a Doctrine entity, save it!
401-
// $entityManager = $this->getDoctrine()->getManager();
402-
// $entityManager->persist($task);
403-
// $entityManager->flush();
404400

405401
return $this->redirectToRoute('task_success');
406402
}

introduction/from_flat_php_to_symfony.rst

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -540,24 +540,21 @@ them for you. Here's the same sample application, now built in Symfony::
540540
namespace App\Controller;
541541

542542
use App\Entity\Post;
543+
use Doctrine\Persistence\ManagerRegistry;
543544
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
544545

545546
class BlogController extends AbstractController
546547
{
547-
public function list()
548+
public function list(ManagerRegistry $doctrine)
548549
{
549-
$posts = $this->getDoctrine()
550-
->getRepository(Post::class)
551-
->findAll();
550+
$posts = $doctrine->getRepository(Post::class)->findAll();
552551

553552
return $this->render('blog/list.html.twig', ['posts' => $posts]);
554553
}
555554

556-
public function show($id)
555+
public function show(ManagerRegistry $doctrine, $id)
557556
{
558-
$post = $this->getDoctrine()
559-
->getRepository(Post::class)
560-
->find($id);
557+
$post = $doctrine->getRepository(Post::class)->find($id);
561558

562559
if (!$post) {
563560
// cause the 404 page not found to be displayed

messenger.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ You're ready! To dispatch the message (and call the handler), inject the
9999
// will cause the SmsNotificationHandler to be called
100100
$bus->dispatch(new SmsNotification('Look! I created a message!'));
101101

102-
// or use the shortcut
103-
$this->dispatchMessage(new SmsNotification('Look! I created a message!'));
104-
105102
// ...
106103
}
107104
}

reference/configuration/doctrine.rst

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,22 @@ which is the first one defined or the one configured via the
155155
``default_connection`` parameter.
156156

157157
Each connection is also accessible via the ``doctrine.dbal.[name]_connection``
158-
service where ``[name]`` is the name of the connection. In a controller
159-
extending ``AbstractController``, you can access it directly using the
160-
``getConnection()`` method and the name of the connection::
161-
162-
$connection = $this->getDoctrine()->getConnection('customer');
163-
164-
$result = $connection->fetchAll('SELECT name FROM customer');
158+
service where ``[name]`` is the name of the connection. In a :doc:`controller </controller>`
159+
you can access it using the ``getConnection()`` method and the name of the connection::
160+
161+
// src/Controller/SomeController.php
162+
use Doctrine\Persistence\ManagerRegistry;
163+
164+
class SomeController
165+
{
166+
public function someMethod(ManagerRegistry $doctrine)
167+
{
168+
$connection = $doctrine->getConnection('customer');
169+
$result = $connection->fetchAll('SELECT name FROM customer');
170+
171+
// ...
172+
}
173+
}
165174

166175
Doctrine ORM Configuration
167176
--------------------------

0 commit comments

Comments
 (0)