G
G
grabbee2016-01-28 21:45:43
symfony
grabbee, 2016-01-28 21:45:43

Should I move getting ORM EntityManager to __constructor?

Should I move getting ORM EntityManager in controller to __constructor ?
Here is an example of code, most of which is duplicated (part of the standard CRUD). Previously, I took out such pieces in the constructor. And how to do this in a symphony?

public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ApiBundle:Upload')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Upload entity.');
        }
        //... 
    }

    /**
     * Displays a form to edit an existing Upload entity. 
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ApiBundle:Upload')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Upload entity.');
        }
        //... 
    }

How to optimize this?
I tried to import the dependency through services, but as it turned out, this cannot be done if the controller is inherited from Symfony\Bundle\FrameworkBundle\Controller\Controller (?)
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class PhotoController extends Controller
{ 
    public function __construct(EntityManager $entityManager)
    {
        //...
    }

src/ApiBundle/Resources/config/services.yml
services:
    api.manager:
      class:  ApiBundle\Controller\PhotoController 
      arguments: [ @doctrine.orm.entity_manager ]

Catchable Fatal Error: Argument 1 passed to ApiBundle\Controller\PhotoController::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in ...
Property Injection is probably not the most elegant solution
symfony.com/doc/ current/components/dependency_inje...

There are mainly only disadvantages to using property injection, it is similar to setter injection but with these additional important problems:
You cannot control when the dependency is set at all, it can be changed at any point in the object's lifetime.
You cannot use type hinting so you cannot be sure what dependency is injected except by writing into the class code to explicitly test the class instance before using it.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
bears, 2016-01-28
@grabbee

/**
 * @Route("/url/{id}")
 */
public function editAction(Upload $upload)
{
  ...
}

Better this way. If the entity is not found, then there will be a 404 error
symfony.com/doc/current/bundles/SensioFrameworkExt...

D
Denis, 2016-02-01
@prototype_denis

1. Since you have decided to make the controller a service, go here symfony.com/doc/current/cookbook/controller/servic...
2. Make the repository a service.
(They forgot about routing.)
3. ParamConverter
4. Inherit from the abstract controller and implement the "smarter" method to get the repository

O
Oleg Krasavin, 2016-02-24
@okwinza

You can also use docs.sylius.org/en/latest/bundles/SyliusResourceBundle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question