title: Inject Symfony EntityManager into Form Type via Services
body: <p>I need to modify some fields in my form (label and class), based on whether the entity is the latest published version or not. So I need to be able to inject the entity manager into my formType so that in an event listener I can compare the current version with the published version of the entity. But I can't even get the <code>entityManager</code> into the __construct() to begin with. Maybe there is a better way of achieving my big goal as well (e.g. modify the form in the twig template), but I NEED to understand how to do this basic dependency inject as well.</p>
  
  <p>I thought that if I declare it in my service (like the documentation describes for basic <a href="http://symfony.com/doc/current/book/service_container.html" rel="nofollow noreferrer">Service Container</a> and specifically <a href="http://symfony.com/doc/current/components/dependency_injection/types.html" rel="nofollow noreferrer">Constructor Injection</a> methods), it will be available as an argument in my construct. But when I do this, I get the error:</p>
  
  <p><code>Catchable fatal error: Argument 1 passed to Gutensite\CmsBundle\Form\Type\ViewType::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /var/www/core/cms/src/Gutensite/ArticleBundle/Controller/AdminEditController.php on line 222 and defined in /var/www/core/cms/src/Gutensite/CmsBundle/Form/Type/ViewType.php on line 15</code></p>
  
  <p>Here are the snippets from my code:</p>
  
  <p><strong>Gutensite/CmsBundle/Resources/config/service.yml</strong></p>
  
  <pre><code>gutensite_cms.form.type.view:
      class: Gutensite\CmsBundle\Form\Type\ViewType
      arguments: [ "@doctrine.orm.entity_manager" ]
  </code></pre>
  
  <p><strong>Gutensite/CmsBundle/Form/Type/ViewType.php</strong></p>
  
  <pre><code>namespace Gutensite\CmsBundle\Form\Type;
  use Doctrine\ORM\EntityManager;
  use Symfony\Component\Form\AbstractType;
  
  class ViewType extends AbstractType
  {
  
      private $em;
  
      public function __construct(EntityManager $entityManager) {
          $this-&gt;em = $entityManager;
      }
  }
  </code></pre>
  
  <p><strong>Gutensite/ArticleBundle/Controller/AdminEditController.php</strong></p>
  
  <pre><code>// Get the View Entity
  $em = $this-&gt;getDoctrine()-&gt;getManager();
  $viewRepo = $em-&gt;getRepository("GutensiteCmsBundle:View\View");
  $view = $viewRepo-&gt;find($request-&gt;query-&gt;get('id'));
  
  // Create the generic form for editing any View, using the view entity constructed
  $form = $this-&gt;createForm(new ViewType(), $view);
  </code></pre>
  
  <h2>NOTE:</h2>
  
  <p>I am using two entity managers, so my config.yml looks something like this. I don't know if that makes any difference in what I inject, i.e. can I inject <code>@doctrine.orm.entity_manager</code> or am I supposed to inject <code>@doctrine.orm.default_entity_manager</code> or something? I've tried all sorts of options and none work.</p>
  
  <pre><code># Doctrine Configuration
  doctrine:
      dbal:
          default_connection: cms
          connections:
              cms:
                  driver:   "%db.cms.driver%"
                  host:     "%db.cms.host%"
                  port:     "%db.cms.port%"
                  dbname:   "%db.cms.name%"
                  user:     "%db.cms.user%"
                  password: "%db.cms.password%"
                  charset:  "%db.cms.charset%"
              billing:
                  driver:   "%db.billing.driver%"
                  host:     "%db.billing.host%"
                  port:     "%db.billing.port%"
                  dbname:   "%db.billing.name%"
                  user:     "%db.billing.user%"
                  password: "%db.billing.password%"
                  charset:  "%db.billing.charset%"
      orm:
          default_entity_manager: cms
          entity_managers:
              cms:
                  connection: cms
                  mappings:
                      GutensiteCmsBundle: ~
                      GutensiteArticleBundle: ~
              billing:
                  connection: billing
                  mappings:
                      GutensiteBillingBundle: ~
          auto_generate_proxy_classes: "%kernel.debug%"
  </code></pre>
  
  <h2>Referenced Already:</h2>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/10427282/symfony-2-entitymanager-injection-in-service">Symfony 2 EntityManager injection in service</a></li>
  <li><a href="https://stackoverflow.com/questions/23155159/symfony2-inject-entitymananager-in-formtype">Symfony2 inject EntityMananager in FormType</a></li>
  </ul>
  
  <h1>Solution:</h1>
  
  <p>I didn't need to define ViewType as a service, I just needed to pass in the entity manager via <code>new viewType($em)</code> when I created the new ViewType form:</p>
  
  <p><strong>Gutensite/ArticleBundle/Controller/AdminEditController.php</strong></p>
  
  <pre><code>// Get the View Entity
  $em = $this-&gt;getDoctrine()-&gt;getManager();
  $viewRepo = $em-&gt;getRepository("GutensiteCmsBundle:View\View");
  $view = $viewRepo-&gt;find($request-&gt;query-&gt;get('id'));
  
  // Create the generic form for editing any View, using the view entity constructed
  $form = $this-&gt;createForm(new ViewType($em), $view);
  </code></pre>