0

Im trying to add entity manager to my service layer in Symfony. I've researched online but none of the answers seem to work for me.

I have a class with a namespace called AppBundle\Controller. My class name is MasterController. Then what exactly do I need to add into my services.yml?

Other than that I think I only need to pass EntityManager $em into the constructor as a param and then assign $em to $this->em (private)?

E.g.

class MasterController extends Controller
{
    private $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    } 
}

2 Answers 2

1

It's possible to define controllers as services while at the same time, extending the framework controller class. Why do this? By doing this you can take advantage of the base functionality such as the form factory, template rendering, get user stuff etc.

At the same time, you can inject any controller specific services you need. This avoids using the container as a service locator pattern. In other words, we can push services into the controller as opposed to having the controllers pull the services. The controllers don't need to care where the services come from and become more self contained.

Example:

class PersonController extends Controller
{
    private $personRepository;

    public function __construct($personRepository)
    {
        $this->personRepository = $personRepository;
    }
    public function editAction(Request $request, $id)
    {
        $person = $this->personRepository->find($id); 

        $form = $this->createForm(new PersonFormType(), $person);

        $form->handleRequest($request);

        if ($form->isValid()) {

            // Note: I add flush/persist methods to my repositories
            // You could inject the entity manager and flush from it
            $personRepository->flush();

            return $this->redirect($this->generateUrl('wherever'));
        }

        return $this->render('person/edit.html.twig', array(
        'form' => $form->createView(),));
    }
}

Make the controller a service with:

services:
person_repository:
    class: AppBundle\Entity\PersonRepository
    factory_service: 'doctrine.orm.default_entity_manager'
    factory_method:  'getRepository'
    arguments:  
        - 'AppBundle\Entity\Person'

person_controller:
    class: AppBundle\Controller\PersonController
    calls:
      - [setContainer,['@service_container']]
    arguments: 
      -'person_repository

So basically, you end up with a standard Symfony controller with all the standard functionality but you can inject controller specific services such as repositories which in turn makes your controllers easier to read and debug. You should never need to use $this->get('service') from within your controller action methods.

Contract this with the more standard method of retrieving an entity:

$personRepository = $this->getDoctrine()->getManager()->getRepository('AppBundle\Entity\Person');

As you can see, the standard method not only ties the controller directly to doctrine but also requires the controller to know the entity's class name. You can decide which method is easier to write, understand and maintain.

0

First of all if your MasterController extends Symfony\Bundle\FrameworkBundle\Controller class then you have already EntityManager available by using:

$this->em = $this->getDoctrine();

But if you want to inject it by yourself to your constructor (and have Controller as a service) all you need to do is have entry in your services.yml like this:

services:
    your_controller_service:
        class: AppBundle\Controller\MasterController
        arguments: [@doctrine.orm.entity_manager]

And that's it.

EDIT:

Remember that you need to have use Doctrine\ORM\EntityManager somewhere before your class definition to avoid Catchable fatal error

6
  • Hmm no I couldn't access em by doing $this->em = $this->getDoctrine(), it gives me Error: Call to a member function has() on a non-object
    – gimcc
    Mar 15, 2015 at 12:08
  • and if I add a service in services.yml then I get Catchable Fatal Error: Argument 1 passed to AppBundle\Controller\MasterController::__construct() must be an instance of AppBundle\Controller\EntityManager, instance of Doctrine\ORM\EntityManager give
    – gimcc
    Mar 15, 2015 at 12:10
  • Defining controllers as services is not such a good practice for a "normal" symfony2 user. Of course he can define it as a service and at the same time extend the symfony's controller, but there is no benefit from that, because controller will be container aware. If you define controller as a service, it usually means that you want to make controller decoupled from the framework. Usually when you only extend symfony's controller, you can get entity manager with how the author of this answer wrote, but make sure you have doctrine installed. Check composer.json if you installed symfony this way.
    – tomazahlin
    Mar 15, 2015 at 12:13
  • Isn't the EntityManager available in an extended controller with $em = $this->getDoctrine()->getManager();?
    – geoB
    Mar 15, 2015 at 12:21
  • 1
    Yup, but not in the constructor. I can only do this in a method. I think I need to do a bit more reading on Symfony 2 :)
    – gimcc
    Mar 15, 2015 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.