title: How to call Entity Manager in a constructor?
body: <p>I've been trying to call <code>Entity Manager</code> in a constructor:</p>
  
  <pre><code>function __construct()
  {
      $this-&gt;getDoctrine()-&gt;getEntityManager();
      ...
  </code></pre>
  
  <p>but, as I've seen in this answer: <a href="https://stackoverflow.com/questions/10480299/call-to-a-member-function-has-on-a-non-object-from-symfony-2-controller">Stackoverflow question</a>, it can't be done.</p>
  
  <p>So I wonder if there is a way to achieve it, as I have to call it often, and want to do some stuff in the constructor after getting the repository. </p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I've tried with @MKhalidJunaid answer:</p>
  
  <pre><code>//src/MSD/HomeBundle/Resources/config/services.yml
  services:
    imageTransController.custom.service:
      class:  MSD\HomeBundle\Controller\ImageTransController
      arguments: 
          EntityManager: "@doctrine.orm.entity_manager"
  </code></pre>
  
  <p>-</p>
  
  <pre><code>//app/config/config.php
  imports:
  - { resource: parameters.yml }
  - { resource: security.yml }
  - { resource: doctrine_extensions.yml }
  - { resource: "@MSDHomeBundle/Resources/config/services.yml" }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>//src/MSD/HomeBundle/Controller/ImageTransController.php
  namespace MSD\HomeBundle\Controller;
  
  use Doctrine\ORM\EntityManager;
  use MSD\HomeBundle\Entity\Imagen as Imagen;
  use MSD\HomeBundle\Controller\HomeController as HomeController;
  
  
  class ImageTransController extends HomeController
  {
      protected $em ;
  
      function __construct(EntityManager $entityManager)
      {
      ...
  </code></pre>
  
  <p>but I'm getting this error:</p>
  
  <pre><code>Catchable Fatal Error: Catchable Fatal Error: Argument 1 passed to MSD\HomeBundle\Controller\ImageTransController::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /home/manolo/MiServer/itransformer/app/cache/dev/jms_diextra/controller_injectors/MSDHomeBundleControllerImageTransController.php on line 13 and defined in /home/manolo/MiServer/itransformer/src/MSD/HomeBundle/Controller/ImageTransController.php line 38 (500 Internal Server Error)
  </code></pre>
  
  <p><strong>New attempt:</strong></p>
  
  <p>I've also tried with @praxmatig answer:</p>
  
  <pre><code>//services.yml
  parameters:
   msd.controller.imagetrans.class: MSD\HomeBundle\Controller\ImageTransController
  
  services:
    msd.imagetrans.controller:
      class:  "%msd.controller.imagetrans.class%"
      arguments: [ @doctrine.orm.entity_manager  ]
  </code></pre>
  
  <p>-</p>
  
  <pre><code>//ImageTransController.php
  namespace MSD\HomeBundle\Controller;
  
   use Doctrine\ORM\EntityManager;
  
  class ImageTransController 
   {
      protected $em ;
  
      function __construct(EntityManager $em)
      {
          $this-&gt;em = $em;
      }
       ...
  </code></pre>
  
  <p>-</p>
  
  <pre><code>//routing.yml
  msd_home_cambiardimensiones:
      pattern: /cambiardimensiones
      defaults: { _controller: MSDHomeBundle:msd.imagetrans.controller:cambiardimensionesAction }
  </code></pre>
  
  <p>but I get this error:</p>
  
  <pre><code> Unable to find controller &amp;quot;MSDHomeBundle:msd.imagetrans.controller&amp;quot; - class &amp;quot;MSD\HomeBundle\Controller\msd.imagetrans.controllerController&amp;quot; does not exist. (500 Internal Server Error)
  </code></pre>