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 entityManager
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.
I thought that if I declare it in my service (like the documentation describes for basic Service Container and specifically Constructor Injection methods), it will be available as an argument in my construct. But when I do this, I get the error:
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
Here are the snippets from my code:
Gutensite/CmsBundle/Resources/config/service.yml
gutensite_cms.form.type.view:
class: Gutensite\CmsBundle\Form\Type\ViewType
arguments: [ "@doctrine.orm.entity_manager" ]
Gutensite/CmsBundle/Form/Type/ViewType.php
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->em = $entityManager;
}
}
Gutensite/ArticleBundle/Controller/AdminEditController.php
// Get the View Entity
$em = $this->getDoctrine()->getManager();
$viewRepo = $em->getRepository("GutensiteCmsBundle:View\View");
$view = $viewRepo->find($request->query->get('id'));
// Create the generic form for editing any View, using the view entity constructed
$form = $this->createForm(new ViewType(), $view);
NOTE:
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 @doctrine.orm.entity_manager
or am I supposed to inject @doctrine.orm.default_entity_manager
or something? I've tried all sorts of options and none work.
# 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%"
Referenced Already:
Solution:
I didn't need to define ViewType as a service, I just needed to pass in the entity manager via new viewType($em)
when I created the new ViewType form:
Gutensite/ArticleBundle/Controller/AdminEditController.php
// Get the View Entity
$em = $this->getDoctrine()->getManager();
$viewRepo = $em->getRepository("GutensiteCmsBundle:View\View");
$view = $viewRepo->find($request->query->get('id'));
// Create the generic form for editing any View, using the view entity constructed
$form = $this->createForm(new ViewType($em), $view);