我正在嘗試為我的物體構建一個多對多關系的標簽系統,
我必須在我添加 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
]);
}
這是我的標簽型別檔案:
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.
似乎我的方法顯然不起作用,錯誤訊息非常明確,但我很難理解我遺漏了什么,我不完全了解服務接執行緒序。
uj5u.com熱心網友回復:
也許Doctrine\ORM\EntityManagerInterface在你的QcmType__construct 中使用
或者甚至更好地使用您想要使用的實際 Repository 類(如果它們從Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository服務擴展并自動裝配為服務)?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/322534.html
