繁体   English   中英

无法以 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扩展并自动装配为服务)?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM