@@ -236,11 +236,10 @@ Now you can see this new code in action! Imagine you're inside a controller::
236
236
use AppBundle\Entity\Category;
237
237
use AppBundle\Entity\Product;
238
238
use Symfony\Component\HttpFoundation\Response;
239
- use Doctrine\ORM\EntityManagerInterface;
240
239
241
240
class DefaultController extends Controller
242
241
{
243
- public function createProductAction(EntityManagerInterface $em )
242
+ public function createProductAction()
244
243
{
245
244
$category = new Category();
246
245
$category->setName('Computer Peripherals');
@@ -253,6 +252,7 @@ Now you can see this new code in action! Imagine you're inside a controller::
253
252
// relate this product to the category
254
253
$product->setCategory($category);
255
254
255
+ $em = $this->getDoctrine()->getManager();
256
256
$em->persist($category);
257
257
$em->persist($product);
258
258
$em->flush();
@@ -276,11 +276,10 @@ When you need to fetch associated objects, your workflow looks just like it
276
276
did before. First, fetch a ``$product `` object and then access its related
277
277
``Category `` object::
278
278
279
- use Doctrine\ORM\EntityManagerInterface;
280
-
281
- public function showAction($productId, EntityManagerInterface $em)
279
+ public function showAction($productId)
282
280
{
283
- $product = $em->getRepository('AppBundle:Product')
281
+ $product = $this->getDoctrine()
282
+ ->getRepository('AppBundle:Product')
284
283
->find($productId);
285
284
286
285
$categoryName = $product->getCategory()->getName();
@@ -304,11 +303,10 @@ the category (i.e. it's "lazily loaded").
304
303
305
304
You can also query in the other direction::
306
305
307
- use Doctrine\ORM\EntityManagerInterface;
308
-
309
- public function showProductsAction($categoryId, EntityManagerInterface $em)
306
+ public function showProductsAction($categoryId)
310
307
{
311
- $category = $em->getRepository('AppBundle:Category')
308
+ $category = $this->getDoctrine()
309
+ ->getRepository('AppBundle:Category')
312
310
->find($categoryId);
313
311
314
312
$products = $category->getProducts();
@@ -367,11 +365,11 @@ can avoid the second query by issuing a join in the original query. Add the
367
365
following method to the ``ProductRepository `` class::
368
366
369
367
// src/AppBundle/Repository/ProductRepository.php
370
- use Doctrine\ORM\EntityManagerInterface;
371
-
372
368
public function findOneByIdJoinedToCategory($productId)
373
369
{
374
- $query = $em->createQuery(
370
+ $query = $this->getDoctrine()
371
+ ->getManager()
372
+ ->createQuery(
375
373
'SELECT p, c FROM AppBundle:Product p
376
374
JOIN p.category c
377
375
WHERE p.id = :id'
@@ -387,11 +385,10 @@ following method to the ``ProductRepository`` class::
387
385
Now, you can use this method in your controller to query for a ``Product ``
388
386
object and its related ``Category `` with just one query::
389
387
390
- use Doctrine\ORM\EntityManagerInterface;
391
-
392
- public function showAction($productId, EntityManagerInterface $em)
388
+ public function showAction($productId)
393
389
{
394
- $product = $em->getRepository('AppBundle:Product')
390
+ $product = $this->getDoctrine()
391
+ ->getRepository('AppBundle:Product')
395
392
->findOneByIdJoinedToCategory($productId);
396
393
397
394
$category = $product->getCategory();
0 commit comments