我正在尝试为我的实体建立一个多对多关系的标签系统,
我必须在添加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: AppFormTypeQcmType
arguments: [ "@doctrine.orm.entity_manager" ]
tags:
- { name: form.type }
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true
当尝试加载包含我的表单的模板时,我得到这个错误:
Cannot autowire service "AppFormQcmType": argument "$manager" of method "__construct()" references interface "DoctrinePersistenceObjectManager" but no such service exists. You should maybe alias this interface to the existing "doctrine.orm.default_entity_manager" service.
似乎我的方法显然不起作用,错误消息是相当明确的,但我很难理解我错过了什么,我不完全理解服务的连接过程。
也许在你的QcmType
__construct中使用DoctrineORMEntityManagerInterface
或者更好地使用您想要使用的实际Repository类(如果它们从DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository
扩展并且自动连接为服务)?