无法以 symfony 形式自动装配服务 "App\Form\QcmType":

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

我正在尝试为我的具有多对多关系的实体构建一个标签系统,

我必须在我添加 TagTypeform 的地方形成 QcmType :

    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
    ]);
}

这是我的 Tagtype 文件:

    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
            ]
        );
    }
}

由于我为 ObjectManager 构建了一个构造器,所以我将其包含在我的 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

在尝试加载包含我的表单的模板时出现此错误:

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.

似乎我的方法显然不起作用,错误消息非常明确,但我很难理解我遗漏了什么,我不完全理解服务连接过程。

也许在你的 QcmType __construct

中使用 Doctrine\ORM\EntityManagerInterface

或者更好地使用您要使用的实际存储库 class(如果它们从 Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository 扩展并自动装配为服务)?