@@ -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
276276did 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
305304You 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
367365following 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::
387385Now, you can use this method in your controller to query for a ``Product ``
388386object 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();
0 commit comments