簡體   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