Home > Net >  Cannot autowire service in a symfony form "App\Form\QcmType":
Cannot autowire service in a symfony form "App\Form\QcmType":

Time:10-13

I'm trying to build a taggin system for my entity with a many-to-many relation,

Its i have to form in place the QcmType where im adding the TagTypeform :

    class QcmType extends AbstractType
{

    private $manager;
    public function __construct(ObjectManager $manager)
    {
        $this->manager = $manager;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('question', TextareaType::class, ['label' => 'Question', 'attr' => array('class' => 'bg-transparent'),] )
            ->add('bonne_reponse', TextareaType::class, ['label' => 'Bonne Réponse', 'attr' => array('class' => 'bg-transparent'),])
            ->add('mauvaise_reponse', TextareaType::class,['label' => 'Mauvaise Réponse 1', 'attr' => array('class' => 'bg-transparent'),] )
            ->add('mauvaise_reponse2', TextareaType::class, ['label' => 'Mauvaise Réponse 2', 'attr' => array('class' => 'bg-transparent'),])
            ->add('explication', TextareaType::class, ['label' => 'Explication', 'attr' => array('class' => 'bg-transparent'),])
            ->add('tags', CollectionType::class, [
                'entry_type' => TagType::class,
                'allow_add' => true,
                'allow_delete' => true,
                'required' => false
            ])
    $builder->get('tags')->addModelTransformer(new TagsToCollectionTransformer($this->manager));
}
 public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
       'data_class' => Qcm::class
    ]);
}

Here is my Tagtype file :

    class TagType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Tag::class
            ]
        );
    }
}

Since im building a contrusctor for ObjectManager, i included it in my App/config/services.yaml

services:
    # default configuration for services in *this* file
    app.form.type.qcm:
        class: App\Form\Type\QcmType
        arguments: [ "@doctrine.orm.entity_manager" ]
        tags:
            - { name: form.type }
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true

While trying to load my template containing my form i get this error :

Cannot autowire service "App\Form\QcmType": argument "$manager" of method "__construct()" references interface "Doctrine\Persistence\ObjectManager" but no such service exists. You should maybe alias this interface to the existing "doctrine.orm.default_entity_manager" service.

Seems like my method is clearly not working, the error message is quite explicit but im having a hard time understandng what im missing, i dont fully understand the services wiring process.

CodePudding user response:

Maybe use Doctrine\ORM\EntityManagerInterface in your QcmType __construct

Or even better use the actual Repository class you want to use (if they extend from Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository and are autowired as services)?

  • Related