I need to change some fields in my form (label and class) based on whether the entity is the latest published version or not. Therefore, I need to be able to embed an entity manager in my formType so that in the event listener I can compare the current version with the published version of the object. But I can't even get entityManagerin __construct () to start. There may be a better way to achieve my big goal (for example, changing the shape in the branch template), but I also need to understand how to make this basic dependency as well.
I thought that if I declare this in my service (for example, the documentation describes the basic Service Container and, in particular, Designer injection methods ), it will be available as an argument in my design. But when I do this, I get an error message:
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 of my code:
Gutensite / CmsBundle / Resources / Configurations / 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
$em = $this->getDoctrine()->getManager();
$viewRepo = $em->getRepository("GutensiteCmsBundle:View\View");
$view = $viewRepo->find($request->query->get('id'));
$form = $this->createForm(new ViewType(), $view);
Note:
I use two entity managers, so my config.yml looks something like this. I donβt know if this affects what I enter, that is, I can insert @doctrine.orm.entity_manager, or should I enter @doctrine.orm.default_entity_manageror something else? I tried all kinds of options and no one works.
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%"
Link already:
:
ViewType , new viewType($em), ViewType:
Gutensite/ArticleBundle//AdminEditController.php
$em = $this->getDoctrine()->getManager();
$viewRepo = $em->getRepository("GutensiteCmsBundle:View\View");
$view = $viewRepo->find($request->query->get('id'));
$form = $this->createForm(new ViewType($em), $view);