我正在寻找一种为一个实体实现多个数据转换器的方法。
我在FormType中添加表单,如下所示:
//RegistrationFormType.php
...
$sectionsForm = $factory->createNamed('sections', 'form', null, array(
'label' => false,
'auto_initialize' => false
));
foreach ($sections as $key=>$section) {
$sectionform = $factory->createNamed('section_'.$section->getId(),'form', null, array(
'label' => $section->getName(),
));
$formQuestions = $section->getFormQuestions();
foreach ($formQuestions as $formQuestion) {
$sectionform->add('question_'.$formQuestion->getId(), 'text',array('attr'=>...));
...
}
$sectionsForm->add($sectionform);
}
$form->add($sectionsForm);
$builder->addModelTransformer(new RegistrationFormToArrayTransformer($em));
现在在ModelTransformer中,我正在改变RegistrationForm实体:
$sections['section_'.$sectionId]['question_'.$questionId] = ...
有更优雅的方法吗?我的源代码变得非常混乱,并且想知道我是否可以以某种方式拥有多个阵列变换器,每个实体一个。
答案 0 :(得分:1)
您可以在每个字段上添加变换器yes:
test_data = ['Love', 'python', 'every','time']