8000 minor #8089 Replacing EntityManagerInterface -> $this->getDoctrine()-… · symfony/symfony-docs@9439177 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9439177

Browse files
committed
minor #8089 Replacing EntityManagerInterface -> $this->getDoctrine()->getManager(); (weaverryan)
This PR was squashed before being merged into the 3.3 branch (closes #8089). Discussion ---------- Replacing EntityManagerInterface -> $this->getDoctrine()->getManager(); When I was updating everything to use type-hints for 3.3, we had discussed that we should probably recommend using the helper method `$this->getDoctrine()` in the controller instead of type-hinting `EntityManagerInterface`. This reverts my changes to reflect that. Commits ------- 32996bf Replacing EntityManagerInterface -> ->getDoctrine()->getManager();
2 parents 70b7fab + 32996bf commit 9439177

10 files changed

+50
-53
lines changed

best_practices/controllers.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ for the homepage of our app:
9797
9898
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9999
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
100-
use Doctrine\ORM\EntityManagerInterface;
101100
102101
class DefaultController extends Controller
103102
{
104103
/**
105104
* @Route("/", name="homepage")
106105
*/
107-
public function indexAction(EntityManagerInterface $em)
106+
public function indexAction()
108107
{
109-
$posts = $em->getRepository('AppBundle:Post')
108+
$posts = $this->getDoctrine()
109+
->getRepository('AppBundle:Post')
110110
->findLatest();
111111
112112
return $this->render('default/index.html.twig', array(

best_practices/security.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,10 @@ more advanced use-case, you can always do the same security check in PHP:
224224
/**
225225
* @Route("/{id}/edit", name="admin_post_edit")
226226
*/
227-
public function editAction($id, EntityManagerInterface $em)
227+
public function editAction($id)
228228
{
229-
$post = $em->getRepository('AppBundle:Post')
229+
$post = $this->getDoctrine()
230+
->getRepository('AppBundle:Post')
230231
->find($id);
231232
232233
if (!$post) {

doctrine.rst

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,12 @@ a controller, this is pretty easy. Add the following method to the
549549
use AppBundle\Entity\Product;
550550
use Symfony\Component\HttpFoundation\Response;
551551
use Doctrine\ORM\EntityManagerInterface;
552-
use Doctrine\Common\Persistence\ManagerRegistry;
553552

554-
public function createAction(EntityManagerInterface $em)
553+
public function createAction()
555554
{
556-
// or fetch the em via the container
557-
// $em = $this->get('doctrine')->getManager();
555+
// you can fetch the EntityManager via $this->getDoctrine()
556+
// or you can add an argument to your action: createAction(EntityManagerInterface $em)
557+
$em = $this->get('doctrine')->getManager();
558558

559559
$product = new Product();
560560
$product->setName('Keyboard');
@@ -571,8 +571,9 @@ a controller, this is pretty easy. Add the following method to the
571571
}
572572

573573
// if you have multiple entity managers, use the registry to fetch them
574-
public function editAction(ManagerRegistry $doctrine)
574+
public function editAction()
575575
{
576+
$doctrine = $this->getDoctrine();
576577
$em = $doctrine->getManager();
577578
$em2 = $doctrine->getManager('other_connection')
578579
}
@@ -586,7 +587,7 @@ Take a look at the previous example in more detail:
586587

587588
.. _doctrine-entity-manager:
588589

589-
* **line 10** The ``EntityManagerInterface`` type-hint tells Symfony to pass you Doctrine's
590+
* **line 13** The ``$this->getDoctrine()->getManager()`` method gets Doctrine's
590591
*entity manager* object, which is the most important object in Doctrine. It's
591592
responsible for saving objects to, and fetching objects from, the database.
592593

@@ -633,11 +634,10 @@ Fetching an object back out of the database is even easier. For example,
633634
suppose you've configured a route to display a specific ``Product`` based
634635
on its ``id`` value::
635636

636-
use Doctrine\ORM\EntityManagerInterface;
637-
638-
public function showAction($productId, EntityManagerInterface $em)
637+
public function showAction($productId)
639638
{
640-
$product = $em->getRepository('AppBundle:Product')
639+
$product = $this->getDoctrine()
640+
->getRepository('AppBundle:Product')
641641
->find($productId);
642642

643643
if (!$product) {
@@ -727,11 +727,11 @@ Updating an Object
727727
Once you've fetched an object from Doctrine, updating it is easy. Suppose
728728
you have a route that maps a product id to an update action in a controller::
729729

730-
use Doctrine\ORM\EntityManagerInterface;
731-
732-
public function updateAction($productId, EntityManagerInterface $em)
730+
public function updateAction($productId)
733731
{
734-
$product = $em->getRepository('AppBundle:Product')->find($productId);
732+
$product = $this->getDoctrine()
733+
->getRepository('AppBundle:Product')
734+
->find($productId);
735735

736736
if (!$product) {
737737
throw $this->createNotFoundException(

doctrine/associations.rst

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,10 @@ Now you can see this new code in action! Imagine you're inside a controller::
236236
use AppBundle\Entity\Category;
237237
use AppBundle\Entity\Product;
238238
use Symfony\Component\HttpFoundation\Response;
239-
use Doctrine\ORM\EntityManagerInterface;
240239

241240
class DefaultController extends Controller
242241
{
243-
public function createProductAction(EntityManagerInterface $em)
242+
public function createProductAction()
244243
{
245244
$category = new Category();
246245
$category->setName('Computer Peripherals');
@@ -253,6 +252,7 @@ Now you can see this new code in action! Imagine you're inside a controller::
253252
// relate this product to the category
254253
$product->setCategory($category);
255254

255+
$em = $this->getDoctrine()->getManager();
256256
$em->persist($category);
257257
$em->persist($product);
258258
$em->flush();
@@ -276,11 +276,10 @@ When you need to fetch associated objects, your workflow looks just like it
276276
did before. First, fetch a ``$product`` object and then access its related
277277
``Category`` object::
278278

279-
use Doctrine\ORM\EntityManagerInterface;
280-
281-
public function showAction($productId, EntityManagerInterface $em)
279+
public function showAction($productId)
282280
{
283-
$product = $em->getRepository('AppBundle:Product')
281+
$product = $this->getDoctrine()
282+
->getRepository('AppBundle:Product')
284283
->find($productId);
285284

286285
$categoryName = $product->getCategory()->getName();
@@ -304,11 +303,10 @@ the category (i.e. it's "lazily loaded").
304303

305304
You can also query in the other direction::
306305

307-
use Doctrine\ORM\EntityManagerInterface;
308-
309-
public function showProductsAction($categoryId, EntityManagerInterface $em)
306+
public function showProductsAction($categoryId)
310307
{
311-
$category = $em->getRepository('AppBundle:Category')
308+
$category = $this->getDoctrine()
309+
->getRepository('AppBundle:Category')
312310
->find($categoryId);
313311

314312
$products = $category->getProducts();
@@ -367,11 +365,11 @@ can avoid the second query by issuing a join in the original query. Add the
367365
following method to the ``ProductRepository`` class::
368366

369367
// src/AppBundle/Repository/ProductRepository.php
370-
use Doctrine\ORM\EntityManagerInterface;
371-
372368
public function findOneByIdJoinedToCategory($productId)
373369
{
374-
$query = $em->createQuery(
370+
$query = $this->getDoctrine()
371+
->getManager()
372+
->createQuery(
375373
'SELECT p, c FROM AppBundle:Product p
376374
JOIN p.category c
377375
WHERE p.id = :id'
@@ -387,11 +385,10 @@ following method to the ``ProductRepository`` class::
387385
Now, you can use this method in your controller to query for a ``Product``
388386
object and its related ``Category`` with just one query::
389387

390-
use Doctrine\ORM\EntityManagerInterface;
391-
392-
public function showAction($productId, EntityManagerInterface $em)
388+
public function showAction($productId)
393389
{
394-
$product = $em->getRepository('AppBundle:Product')
390+
$product = $this->getDoctrine()
391+
->getRepository('AppBundle:Product')
395392
->findOneByIdJoinedToCategory($productId);
396393

397394
$category = $product->getCategory();

doctrine/registration_form.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,13 @@ into the database::
226226
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
227227
use Symfony\Component\HttpFoundation\Request;
228228
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
229-
use Doctrine\ORM\EntityManagerInterface;
230229

231230
class RegistrationController extends Controller
232231
{
233232
/**
234233
* @Route("/register", name="user_registration")
235234
*/
236-
public function registerAction(Request $request, UserPasswordEncoderInterface $passwordEncoder, EntityManagerInterface $em)
235+
public function registerAction(Request $request, UserPasswordEncoderInterface $passwordEncoder)
237236
{
238237
// 1) build the form
239238
$user = new User();
@@ -248,6 +247,7 @@ into the database::
248247
$user->setPassword($password);
249248

250249
// 4) save the User!
250+
$em = $this->getDoctrine()->getManager();
251251
$em->persist($user);
252252
$em->flush();
253253

doctrine/repository.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ entities, ordered alphabetically by name.
9696

9797
You can use this new method just like the default finder methods of the repository::
9898

99-
use Doctrine\ORM\EntityManagerInterface;
10099
// ...
101100

102-
public function listAction(EntityManagerInterface $em)
101+
public function listAction()
103102
{
104-
$products = $em->getRepository('AppBundle:Product')
103+
$products = $this->getDoctrine()
104+
->getRepository('AppBundle:Product')
105105
->findAllOrderedByName();
106106
}
107107

form/form_collections.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,11 +679,11 @@ the relationship between the removed ``Tag`` and ``Task`` object.
679679
// src/AppBundle/Controller/TaskController.php
680680

681681
use Doctrine\Common\Collections\ArrayCollection;
682-
use Doctrine\ORM\EntityManagerInterface;
683682

684683
// ...
685-
public function editAction($id, Request $request, EntityManagerInterface $e,)
684+
public function editAction($id, Request $request)
686685
{
686+
$em = $this->getDoctrine()->getManager();
687687
$task = $em->getRepository('AppBundle:Task')->find($id);
688688

689689
if (!$task) {

form/form_dependencies.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@ create your form::
3232

3333
// src/AppBundle/Controller/DefaultController.php
3434
use AppBundle\Form\TaskType;
35-
use Doctrine\ORM\EntityManagerInterface;
3635

3736
// ...
38-
public function newAction(EntityManagerInterface $em)
37+
public function newAction()
3938
{
40-
// or fetch the em via the container
41-
// $em = $this->get('doctrine')->getManager();
39+
$em = $this->getDoctrine()->getManager();
4240

4341
$task = ...;
4442
$form = $this->createForm(TaskType::class, $task, array(

forms.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ your controller::
221221
// ...
222222
use Symfony\Component\HttpFoundation\Request;
223223

224-
public function newAction(Request $request, EntityManagerInterface $em)
224+
public function newAction(Request $request)
225225
{
226226
// just setup a fresh $task object (remove the dummy data)
227227
$task = new Task();
@@ -241,6 +241,7 @@ your controller::
241241

242242
// ... perform some action, such as saving the task to the database
243243
// for example, if Task is a Doctrine entity, save it!
244+
// $em = $this->getDoctrine()->getManager();
244245
// $em->persist($task);
245246
// $em->flush();
246247

introduction/from_flat_php_to_symfony2.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,22 +545,22 @@ them for you. Here's the same sample application, now built in Symfony::
545545
namespace AppBundle\Controller;
546546

547547
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
548-
use Doctrine\ORM\EntityManagerInterface;
549548

550549
class BlogController extends Controller
551550
{
552-
public function listAction(EntityManagerInterface $em)
551+
public function listAction()
553552
{
554-
$posts = $em
553+
$posts = $this->getDoctrine()
554+
->getManager()
555555
->createQuery('SELECT p FROM AppBundle:Post p')
556556
->execute();
557557

558558
return $this->render('Blog/list.html.php', array('posts' => $posts));
559559
}
560560

561-
public function showAction(EntityManagerInterface $em)
561+
public function showAction()
562562
{
563-
$post = $em
563+
$post = $this->getDoctrine()
564564
->getRepository('AppBundle:Post')
565565
->find($id);
566566

0 commit comments

Comments
 (0)
0