@@ -549,17 +549,19 @@ a controller, this is pretty easy. Add the following method to the
549
549
use AppBundle\Entity\Product;
550
550
use Symfony\Component\HttpFoundation\Response;
551
551
use Doctrine\ORM\EntityManagerInterface;
552
+ use Doctrine\Common\Persistence\ManagerRegistry;
552
553
553
554
// ...
554
- public function createAction()
555
+ public function createAction(EntityManagerInterface $em )
555
556
{
557
+ // or fetch the em via the container
558
+ // $em = $this->get('doctrine')->getManager();
559
+
556
560
$product = new Product();
557
561
$product->setName('Keyboard');
558
562
$product->setPrice(19.99);
559
563
$product->setDescription('Ergonomic and stylish!');
560
564
561
- $em = $this->getDoctrine()->getManager();
562
-
563
565
// tells Doctrine you want to (eventually) save the Product (no queries yet)
564
566
$em->persist($product);
565
567
@@ -569,26 +571,18 @@ a controller, this is pretty easy. Add the following method to the
569
571
return new Response('Saved new product with id '.$product->getId());
570
572
}
571
573
572
- // you can also receive the $em as an argument
573
- public function editAction(EntityManagerInterface $em )
574
+ // if you have multiple entity managers, use the registry to fetch them
575
+ public function editAction(ManagerRegistry $doctrine )
574
576
{
575
- // ...
577
+ $em = $doctrine->getManager();
578
+ $em2 = $doctrine->getManager('other_connection')
576
579
}
577
580
578
581
.. note ::
579
582
580
583
If you're following along with this example, you'll need to create a
581
584
route that points to this action to see it work.
582
585
583
- .. tip ::
584
-
585
- This article shows working with Doctrine from within a controller by using
586
- the :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::getDoctrine `
587
- method of the controller. This method is a shortcut to get the
588
- ``doctrine `` service. You can work with Doctrine anywhere else
589
- by injecting that service in the service. See
590
- :doc: `/service_container ` for more on creating your own services.
591
-
592
586
Take a look at the previous example in more detail:
593
587
594
588
* **lines 10-13 ** In this section, you instantiate and work with the ``$product ``
@@ -638,10 +632,11 @@ Fetching an object back out of the database is even easier. For example,
638
632
suppose you've configured a route to display a specific ``Product `` based
639
633
on its ``id `` value::
640
634
641
- public function showAction($productId)
635
+ use Doctrine\ORM\EnityManagerInterface;
636
+
637
+ public function showAction($productId, EnityManagerInterface $em)
642
638
{
643
- $product = $this->getDoctrine()
644
- ->getRepository('AppBundle:Product')
639
+ $product = $em->getRepository('AppBundle:Product')
645
640
->find($productId);
646
641
647
642
if (!$product) {
@@ -664,8 +659,7 @@ as its "repository". You can think of a repository as a PHP class whose only
664
659
job is to help you fetch entities of a certain class. You can access the
665
660
repository object for an entity class via::
666
661
667
- $repository = $this->getDoctrine()
668
- ->getRepository('AppBundle:Product');
662
+ $repository = $em->getRepository('AppBundle:Product');
669
663
670
664
.. note ::
671
665
@@ -676,7 +670,7 @@ repository object for an entity class via::
676
670
677
671
Once you have a repository object, you can access all sorts of helpful methods::
678
672
679
- $repository = $this->getDoctrine() ->getRepository('AppBundle:Product');
673
+ $repository = $em ->getRepository('AppBundle:Product');
680
674
681
675
// query for a single product by its primary key (usually "id")
682
676
$product = $repository->find($productId);
@@ -699,7 +693,7 @@ Once you have a repository object, you can access all sorts of helpful methods::
699
693
You can also take advantage of the useful ``findBy() `` and ``findOneBy() `` methods
700
694
to easily fetch objects based on multiple conditions::
701
695
702
- $repository = $this->getDoctrine() ->getRepository('AppBundle:Product');
696
+ $repository = $em ->getRepository('AppBundle:Product');
703
697
704
698
// query for a single product matching the given name and price
705
699
$product = $repository->findOneBy(
@@ -732,9 +726,10 @@ Updating an Object
732
726
Once you've fetched an object from Doctrine, updating it is easy. Suppose
733
727
you have a route that maps a product id to an update action in a controller::
734
728
735
- public function updateAction($productId)
729
+ use Doctrine\ORM\EnityManagerInterface;
730
+
731
+ public function updateAction($productId, EntityManagerInterface $em)
736
732
{
737
- $em = $this->getDoctrine()->getManager();
738
733
$product = $em->getRepository('AppBundle:Product')->find($productId);
739
734
740
735
if (!$product) {
@@ -781,7 +776,7 @@ Querying for Objects
781
776
You've already seen how the repository object allows you to run basic queries
782
777
without any work::
783
778
784
- $repository = $this->getDoctrine() ->getRepository('AppBundle:Product');
779
+ $repository = $em ->getRepository('AppBundle:Product');
785
780
786
781
$product = $repository->find($productId);
787
782
$product = $repository->findOneByName('Keyboard');
@@ -801,7 +796,6 @@ Imagine that you want to query for products that cost more than ``19.99``,
801
796
ordered from least to most expensive. You can use DQL, Doctrine's native
802
797
SQL-like language, to construct a query for this scenario::
803
798
804
- $em = $this->getDoctrine()->getManager();
805
799
$query = $em->createQuery(
806
800
'SELECT p
807
801
FROM AppBundle:Product p
@@ -841,8 +835,7 @@ Instead of writing a DQL string, you can use a helpful object called the
841
835
depends on dynamic conditions, as your code soon becomes hard to read with
842
836
DQL as you start to concatenate strings::
843
837
844
- $repository = $this->getDoctrine()
845
- ->getRepository('AppBundle:Product');
838
+ $repository = $em->getRepository('AppBundle:Product');
846
839
847
840
// createQueryBuilder() automatically selects FROM AppBundle:Product
848
841
// and aliases it to "p"
0 commit comments