我在用特性构建实体时遇到了困难,无法为这些实体构建表单。
例如,我的“文章”实体只包含类别链接和2张图片,其余属性位于SeoTrait(标题、meta_title、meta_desc、content等)和ValidTrait(isValid true/false)中,我希望将其用于其他实体。
对于Doctrine来说,这一切都很好,它会为使用这些特性的每个实体生成包含特性字段的模式。但问题出现在表单上:
我已经为“SEO”属性创建了SeoTraitType:
class SeoTraitType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array(
'label' => 'Nom'
))
->add('metaTitle', TextType::class, array(
'label' => 'Meta Title'
))
->add('metaDescription', TextareaType::class, array(
'label' => 'Meta Description'
))
->add('metaKeywords', TextType::class, array(
'label' => 'Keywords'
))
->add('content', TextareaType::class, array(
'label' => 'Content'
))
;
}
}
然后我将其用于我的ArticleType中:
class ArticleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('seo', SeoTraitType::class, array(
'label' => 'Seo',
'mapped' => false
))
->add('isValid', ValidTraitType::class, array(
'label' => 'Valid',
'mapped' => false
))
->add('save', SubmitType::class, array(
'label' => 'form_save',
'translation_domain' => 'back_default'
));
;
}
}
我的问题是,当我想在主实体的表单中嵌入两个TraitTypes时,必须将mapped => false设置为这两个TraitTypes。然后在我的表单中,我使用article[seo][name]来获取SeoTrait的字段,因此我不能真正使用$form->handleRequest()方法来处理表单提交...如何解决这个问题?是否可以在表单组件提供的方法中以特殊方式处理它,还是我必须自己处理请求并解析trait数组以在保存实体之前构建它?我在互联网上找不到任何相关信息:(