-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Replacing EntityManagerInterface -> $this->getDoctrine()->getManager(); #8089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -551,10 +551,11 @@ a controller, this is pretty easy. Add the following method to the | |
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
|
||
public function createAction(EntityManagerInterface $em) | ||
public function createAction() | ||
{ | ||
// or fetch the em via the container | ||
// $em = $this->get('doctrine')->getManager(); | ||
// you can fetch the EntityManager via $this->getDoctrine() | ||
// or you can add an argument to your action: createAction(EntityManagerInterface $em) | ||
$em = $this->get('doctrine')->getManager(); | ||
|
||
$product = new Product(); | ||
$product->setName('Keyboard'); | ||
|
@@ -571,8 +572,9 @@ a controller, this is pretty easy. Add the following method to the | |
} | ||
|
||
// if you have multiple entity managers, use the registry to fetch them | ||
public function editAction(ManagerRegistry $doctrine) | ||
public function editAction() | ||
{ | ||
$doctrine = $this->getDoctrine(); | ||
$em = $doctrine->getManager(); | ||
$em2 = $doctrine->getManager('other_connection') | ||
} | ||
|
@@ -586,7 +588,7 @@ Take a look at the previous example in more detail: | |
|
||
.. _doctrine-entity-manager: | ||
|
||
* **line 10** The ``EntityManagerInterface`` type-hint tells Symfony to pass you Doctrine's | ||
* **line 13** The ``$this->getDoctrine()->getManager()`` method gets Doctrine's | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I verified all the lines are correct via platform.sh |
||
*entity manager* object, which is the most important object in Doctrine. It's | ||
responsible for saving objects to, and fetching objects from, the database. | ||
|
||
|
@@ -633,11 +635,10 @@ Fetching an object back out of the database is even easier. For example, | |
suppose you've configured a route to display a specific ``Product`` based | ||
on its ``id`` value:: | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
public function showAction($productId, EntityManagerInterface $em) | ||
public function showAction($productId) | ||
{ | ||
$product = $em->getRepository('AppBundle:Product') | ||
$product = $this->getDoctrine() | ||
->getRepository('AppBundle:Product') | ||
->find($productId); | ||
|
||
if (!$product) { | ||
|
@@ -727,11 +728,11 @@ Updating an Object | |
Once you've fetched an object from Doctrine, updating it is easy. Suppose | ||
you have a route that maps a product id to an update action in a controller:: | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
public function updateAction($productId, EntityManagerInterface $em) | ||
public function updateAction($productId) | ||
{ | ||
$product = $em->getRepository('AppBundle:Product')->find($productId); | ||
$product = $this->getDoctrine() | ||
->getRepository('AppBundle:Product') | ||
->find($productId); | ||
|
||
if (!$product) { | ||
throw $this->createNotFoundException( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,11 +236,10 @@ Now you can see this new code in action! Imagine you're inside a controller:: | |
use AppBundle\Entity\Category; | ||
use AppBundle\Entity\Product; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
class DefaultController extends Controller | ||
{ | ||
public function createProductAction(EntityManagerInterface $em) | ||
public function createProductAction() | ||
{ | ||
$category = new Category(); | ||
$category->setName('Computer Peripherals'); | ||
|
@@ -253,6 +252,7 @@ Now you can see this new code in action! Imagine you're inside a controller:: | |
// relate this product to the category | ||
$product->setCategory($category); | ||
|
||
$em = $this->getDoctrine()->getManager(); | ||
$em->persist($category); | ||
$em->persist($product); | ||
$em->flush(); | ||
|
@@ -276,11 +276,10 @@ When you need to fetch associated objects, your workflow looks just like it | |
did before. First, fetch a ``$product`` object and then access its related | ||
``Category`` object:: | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
public function showAction($productId, EntityManagerInterface $em) | ||
public function showAction($productId) | ||
{ | ||
$product = $em->getRepository('AppBundle:Product') | ||
$product = $this->getDoctrine() | ||
->getRepository('AppBundle:Product') | ||
->find($productId); | ||
|
||
$categoryName = $product->getCategory()->getName(); | ||
|
@@ -304,11 +303,10 @@ the category (i.e. it's "lazily loaded"). | |
|
||
You can also query in the other direction:: | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
public function showProductsAction($categoryId, EntityManagerInterface $em) | ||
public function showProductsAction($categoryId) | ||
{ | ||
$category = $em->getRepository('AppBundle:Category') | ||
$category = $this->getDoctrine() | ||
->getRepository('AppBundle:Category') | ||
->find($categoryId); | ||
|
||
$products = $category->getProducts(); | ||
|
@@ -367,11 +365,11 @@ can avoid the second query by issuing a join in the original query. Add the | |
following method to the ``ProductRepository`` class:: | ||
|
||
// src/AppBundle/Repository/ProductRepository.php | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
public function findOneByIdJoinedToCategory($productId) | ||
{ | ||
$query = $em->createQuery( | ||
$query = $this->getDoctrine() | ||
->getManager() | ||
->createQuery( | ||
'SELECT p, c FROM AppBundle:Product p | ||
JOIN p.category c | ||
WHERE p.id = :id' | ||
|
@@ -387,11 +385,11 @@ following method to the ``ProductRepository`` class:: | |
Now, you can use this method in your controller to query for a ``Product`` | ||
object and its related ``Category`` with just one query:: | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
public function showAction($productId, EntityManagerInterface $em) | ||
public function showAction($productId) | ||
{ | ||
$product = $em->getRepository('AppBundle:Product') | ||
$product = $this->getDoctrine() | ||
->getManager() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line is useless, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch - removed it :) |
||
->getRepository('AppBundle:Product') | ||
->findOneByIdJoinedToCategory($productId); | ||
|
||
$category = $product->getCategory(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,13 +32,11 @@ create your form:: | |
|
||
// src/AppBundle/Controller/DefaultController.php | ||
use AppBundle\Form\TaskType; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
// ... | ||
public function newAction(EntityManagerInterface $em) | ||
public function newAction() | ||
{ | ||
// or fetch the em via the container | ||
// $em = $this->get('doctrine')->getManager(); | ||
$em = $this->get('doctrine')->getManager(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're not using |
||
|
||
$task = ...; | ||
$form = $this->createForm(TaskType::class, $task, array( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be removed