diff --git a/best_practices/controllers.rst b/best_practices/controllers.rst index 89f6a497493..0c60ef118ee 100644 --- a/best_practices/controllers.rst +++ b/best_practices/controllers.rst @@ -97,16 +97,16 @@ for the homepage of our app: use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; - use Doctrine\ORM\EntityManagerInterface; class DefaultController extends Controller { /** * @Route("/", name="homepage") */ - public function indexAction(EntityManagerInterface $em) + public function indexAction() { - $posts = $em->getRepository('AppBundle:Post') + $posts = $this->getDoctrine() + ->getRepository('AppBundle:Post') ->findLatest(); return $this->render('default/index.html.twig', array( diff --git a/best_practices/security.rst b/best_practices/security.rst index af004171334..76ec9844400 100644 --- a/best_practices/security.rst +++ b/best_practices/security.rst @@ -224,9 +224,10 @@ more advanced use-case, you can always do the same security check in PHP: /** * @Route("/{id}/edit", name="admin_post_edit") */ - public function editAction($id, EntityManagerInterface $em) + public function editAction($id) { - $post = $em->getRepository('AppBundle:Post') + $post = $this->getDoctrine() + ->getRepository('AppBundle:Post') ->find($id); if (!$post) { diff --git a/doctrine.rst b/doctrine.rst index e7750e126ec..69709654245 100644 --- a/doctrine.rst +++ b/doctrine.rst @@ -549,12 +549,12 @@ a controller, this is pretty easy. Add the following method to the use AppBundle\Entity\Product; use Symfony\Component\HttpFoundation\Response; 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 +571,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 +587,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 *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 +634,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 +727,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( diff --git a/doctrine/associations.rst b/doctrine/associations.rst index 6ccdbeae6ec..554d9b38dd9 100644 --- a/doctrine/associations.rst +++ b/doctrine/associations.rst @@ -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,10 @@ 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() + ->getRepository('AppBundle:Product') ->findOneByIdJoinedToCategory($productId); $category = $product->getCategory(); diff --git a/doctrine/registration_form.rst b/doctrine/registration_form.rst index 1fbdf9d50c4..0c336fc29b1 100644 --- a/doctrine/registration_form.rst +++ b/doctrine/registration_form.rst @@ -226,14 +226,13 @@ into the database:: use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; - use Doctrine\ORM\EntityManagerInterface; class RegistrationController extends Controller { /** * @Route("/register", name="user_registration") */ - public function registerAction(Request $request, UserPasswordEncoderInterface $passwordEncoder, EntityManagerInterface $em) + public function registerAction(Request $request, UserPasswordEncoderInterface $passwordEncoder) { // 1) build the form $user = new User(); @@ -248,6 +247,7 @@ into the database:: $user->setPassword($password); // 4) save the User! + $em = $this->getDoctrine()->getManager(); $em->persist($user); $em->flush(); diff --git a/doctrine/repository.rst b/doctrine/repository.rst index acc80431f22..21191bab4de 100644 --- a/doctrine/repository.rst +++ b/doctrine/repository.rst @@ -96,12 +96,12 @@ entities, ordered alphabetically by name. You can use this new method just like the default finder methods of the repository:: - use Doctrine\ORM\EntityManagerInterface; // ... - public function listAction(EntityManagerInterface $em) + public function listAction() { - $products = $em->getRepository('AppBundle:Product') + $products = $this->getDoctrine() + ->getRepository('AppBundle:Product') ->findAllOrderedByName(); } diff --git a/form/form_collections.rst b/form/form_collections.rst index 8f6e1430134..db39bc0ff56 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -679,11 +679,11 @@ the relationship between the removed ``Tag`` and ``Task`` object. // src/AppBundle/Controller/TaskController.php use Doctrine\Common\Collections\ArrayCollection; - use Doctrine\ORM\EntityManagerInterface; // ... - public function editAction($id, Request $request, EntityManagerInterface $e,) + public function editAction($id, Request $request) { + $em = $this->getDoctrine()->getManager(); $task = $em->getRepository('AppBundle:Task')->find($id); if (!$task) { diff --git a/form/form_dependencies.rst b/form/form_dependencies.rst index 91655f59ff6..00378fa2203 100644 --- a/form/form_dependencies.rst +++ b/form/form_dependencies.rst @@ -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->getDoctrine()->getManager(); $task = ...; $form = $this->createForm(TaskType::class, $task, array( diff --git a/forms.rst b/forms.rst index 168025bad91..084aaa63030 100644 --- a/forms.rst +++ b/forms.rst @@ -221,7 +221,7 @@ your controller:: // ... use Symfony\Component\HttpFoundation\Request; - public function newAction(Request $request, EntityManagerInterface $em) + public function newAction(Request $request) { // just setup a fresh $task object (remove the dummy data) $task = new Task(); @@ -241,6 +241,7 @@ your controller:: // ... perform some action, such as saving the task to the database // for example, if Task is a Doctrine entity, save it! + // $em = $this->getDoctrine()->getManager(); // $em->persist($task); // $em->flush(); diff --git a/introduction/from_flat_php_to_symfony2.rst b/introduction/from_flat_php_to_symfony2.rst index 15501cb15a1..31a9a77e6dd 100644 --- a/introduction/from_flat_php_to_symfony2.rst +++ b/introduction/from_flat_php_to_symfony2.rst @@ -545,22 +545,22 @@ them for you. Here's the same sample application, now built in Symfony:: namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; - use Doctrine\ORM\EntityManagerInterface; class BlogController extends Controller { - public function listAction(EntityManagerInterface $em) + public function listAction() { - $posts = $em + $posts = $this->getDoctrine() + ->getManager() ->createQuery('SELECT p FROM AppBundle:Post p') ->execute(); return $this->render('Blog/list.html.php', array('posts' => $posts)); } - public function showAction(EntityManagerInterface $em) + public function showAction() { - $post = $em + $post = $this->getDoctrine() ->getRepository('AppBundle:Post') ->find($id);