Deploy Symfony EntityManager to form type through services

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

// 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 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 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%"

Link already:

:

ViewType , new viewType($em), ViewType:

Gutensite/ArticleBundle//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);
+4
3

, :

$form = $this->createForm(new ViewType(), $view);

ViewType - , EntityManager. , :

$em = $this->get('doctrine.orm.entity_manager'); // or doctrine.orm.billing_entity_manager
$form = $this->createForm(new ViewType($em), $view);

.

doctrine.orm.entity_manager doctrine.orm.billing_entity_manager , ViewType - ( ).

UPDATE:

.

(services.yml):

services
    gutensite_cms.form.view:
        factory_method: createNamed
        factory_service: form.factory
        class: Symfony\Component\Form\Form
        arguments:
            - view_form                        # name of the form
            - view                             # alias of the form type
            - null                             # data to bind, this is where your entity could go if you have that defined as a service
            - { validation_groups: [Default] } # validation groups

    gutensite_cms.form.type.view:
        class: Gutensite\CmsBundle\Form\Type\ViewType
        arguments: [ "@doctrine.orm.entity_manager" ]
        tags:
            - { name: form.type, alias: view }

, ( container) - ( ):

public function newAction()
{
    $view = ...;
    $form = $this->get( 'gutensite_cms.form.view' );

    // set initial form data if needed
    $form->setData( $view );
}
+7

ViewType EntityManager. dic

$form = $this->createForm($this->get('gutensite_cms.form.type.view'), $view);

$form = $this->createForm(new ViewType($this->getDoctrine()->getManager()), $view);
+1

:

...
<parameter key="gutensite_cms.form.type.view.class">Gutensite\CmsBundle\Form\Type\ViewVersionType</parameter
...

...
<service id="gutensite_cms.form.type.view" class="%gutensite_cms.form.type.view.class%">
  <argumet type="service" id="doctrine.orm.CMS_OR_BILLING_entity_manager" />
  <tag name="form.type" alias="YOUR_FORM_TYPE_NAME" />
</service>
...

:

...
private $em;

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

ou :

$this->container->get('doctrine')->getManager('cms'); // or billing
+1

All Articles