mardif
mardif

Reputation: 341

pass entityManager to EntityType class

Using Symfony 3.2, I would pass entityManager object to EntityType class through constructor.

I found that it can been done using services, as my configuration below:

config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
    - { resource: "@mybundle/Resources/config/services.yml" }

services.yml

services:
    mybundle.profile_key:
        class: App\Bundle\mybundle\Form\ProfileKeyType
        arguments: ["@doctrine.orm.entity_manager"]

My EntityType:

namespace App\Bundle\mybundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Doctrine\ORM\EntityManager;

class ProfileKeyType extends AbstractType
{

    private $em;
    public function __construct(EntityManager $em) {
        $this->em = $em;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('key', TextType::class, array(
                "label" => "Chiave identificativa"
        ))->add('visible', CheckboxType::class, array(
                'label' => "Default visible",
                "label_attr" => array( 'title' => 'Imposta la visibilità di default di questa chiave' ),
                'required' => true
        ))->add('entity', CollectionType::class, array(
                "data_class" => Entity::class
        ))->add('property')->add('type');
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\Bundle\mybundle\Entity\ProfileKey'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'app_bundle_mybundle_profilekey';
    }


}

When I lauch my insert page, I get this error:

Type error: Argument 1 passed to App\Bundle\myBundle\Form\ProfileKeyType::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in D:\eclipse_neon_workspace\iSerPa\vendor\symfony\symfony\src\Symfony\Component\Form\FormRegistry.php on line 85

What's wrong??

Thx in advance

Upvotes: 0

Views: 742

Answers (2)

stephan.mada
stephan.mada

Reputation: 1130

When creating the form, you can simply do like this :

// inside controller action
// $em is the required EntityManager instance
$em = $this->getDoctrine()->getEntityManager();
$form = $this->createForm(new ProfileKeyType($em), $profileKey);

Upvotes: 2

Dan Dumitriu
Dan Dumitriu

Reputation: 226

you should add the form.type tag to your service definition like this:

services:
serpabackend.profile_key:
    class: App\Bundle\mybundle\Form\ProfileKeyType
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        - { name: form.type }

Upvotes: 0

Related Questions