Skip to content

Commit 51ad1f5

Browse files
committed
minor #7876 Example of the Doctrine doc with types as the first-class citizen (weaverryan)
This PR was squashed before being merged into the master branch (closes #7876). Discussion ---------- Example of the Doctrine doc with types as the first-class citizen This updates the doctrine chapter in the new "types-first" philosophy - i.e. using type-hinting and autowiring to fetch services (rather than service ids). Of course, we must still show both (the type hint and the service id), but probably only the first time in a chapter. Afterwards, we should show the type. This is the first "normal" chapter to get this update. Thoughts? Thanks! Commits ------- 0f31c77 Example of the Doctrine doc with types as the first-class citizen
2 parents 76424ea + 0f31c77 commit 51ad1f5

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

doctrine.rst

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -549,17 +549,19 @@ 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;
552553

553554
// ...
554-
public function createAction()
555+
public function createAction(EntityManagerInterface $em)
555556
{
557+
// or fetch the em via the container
558+
// $em = $this->get('doctrine')->getManager();
559+
556560
$product = new Product();
557561
$product->setName('Keyboard');
558562
$product->setPrice(19.99);
559563
$product->setDescription('Ergonomic and stylish!');
560564

561-
$em = $this->getDoctrine()->getManager();
562-
563565
// tells Doctrine you want to (eventually) save the Product (no queries yet)
564566
$em->persist($product);
565567

@@ -569,26 +571,18 @@ a controller, this is pretty easy. Add the following method to the
569571
return new Response('Saved new product with id '.$product->getId());
570572
}
571573

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)
574576
{
575-
// ...
577+
$em = $doctrine->getManager();
578+
$em2 = $doctrine->getManager('other_connection')
576579
}
577580

578581
.. note::
579582

580583
If you're following along with this example, you'll need to create a
581584
route that points to this action to see it work.
582585

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-
592586
Take a look at the previous example in more detail:
593587

594588
* **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,
638632
suppose you've configured a route to display a specific ``Product`` based
639633
on its ``id`` value::
640634

641-
public function showAction($productId)
635+
use Doctrine\ORM\EnityManagerInterface;
636+
637+
public function showAction($productId, EnityManagerInterface $em)
642638
{
643-
$product = $this->getDoctrine()
644-
->getRepository('AppBundle:Product')
639+
$product = $em->getRepository('AppBundle:Product')
645640
->find($productId);
646641

647642
if (!$product) {
@@ -664,8 +659,7 @@ as its "repository". You can think of a repository as a PHP class whose only
664659
job is to help you fetch entities of a certain class. You can access the
665660
repository object for an entity class via::
666661

667-
$repository = $this->getDoctrine()
668-
->getRepository('AppBundle:Product');
662+
$repository = $em->getRepository('AppBundle:Product');
669663

670664
.. note::
671665

@@ -676,7 +670,7 @@ repository object for an entity class via::
676670

677671
Once you have a repository object, you can access all sorts of helpful methods::
678672

679-
$repository = $this->getDoctrine()->getRepository('AppBundle:Product');
673+
$repository = $em->getRepository('AppBundle:Product');
680674

681675
// query for a single product by its primary key (usually "id")
682676
$product = $repository->find($productId);
@@ -699,7 +693,7 @@ Once you have a repository object, you can access all sorts of helpful methods::
699693
You can also take advantage of the useful ``findBy()`` and ``findOneBy()`` methods
700694
to easily fetch objects based on multiple conditions::
701695

702-
$repository = $this->getDoctrine()->getRepository('AppBundle:Product');
696+
$repository = $em->getRepository('AppBundle:Product');
703697

704698
// query for a single product matching the given name and price
705699
$product = $repository->findOneBy(
@@ -732,9 +726,10 @@ Updating an Object
732726
Once you've fetched an object from Doctrine, updating it is easy. Suppose
733727
you have a route that maps a product id to an update action in a controller::
734728

735-
public function updateAction($productId)
729+
use Doctrine\ORM\EnityManagerInterface;
730+
731+
public function updateAction($productId, EntityManagerInterface $em)
736732
{
737-
$em = $this->getDoctrine()->getManager();
738733
$product = $em->getRepository('AppBundle:Product')->find($productId);
739734

740735
if (!$product) {
@@ -781,7 +776,7 @@ Querying for Objects
781776
You've already seen how the repository object allows you to run basic queries
782777
without any work::
783778

784-
$repository = $this->getDoctrine()->getRepository('AppBundle:Product');
779+
$repository = $em->getRepository('AppBundle:Product');
785780

786781
$product = $repository->find($productId);
787782
$product = $repository->findOneByName('Keyboard');
@@ -801,7 +796,6 @@ Imagine that you want to query for products that cost more than ``19.99``,
801796
ordered from least to most expensive. You can use DQL, Doctrine's native
802797
SQL-like language, to construct a query for this scenario::
803798

804-
$em = $this->getDoctrine()->getManager();
805799
$query = $em->createQuery(
806800
'SELECT p
807801
FROM AppBundle:Product p
@@ -841,8 +835,7 @@ Instead of writing a DQL string, you can use a helpful object called the
841835
depends on dynamic conditions, as your code soon becomes hard to read with
842836
DQL as you start to concatenate strings::
843837

844-
$repository = $this->getDoctrine()
845-
->getRepository('AppBundle:Product');
838+
$repository = $em->getRepository('AppBundle:Product');
846839

847840
// createQueryBuilder() automatically selects FROM AppBundle:Product
848841
// and aliases it to "p"

0 commit comments

Comments
 (0)