0
我有兩個實體,它們通過多對一的教義關聯相互連接。我創建了一個表單集合,但是當我嘗試保存某些內容時它碰到了一個錯誤。Symfony表單集合錯誤
,我遇到了錯誤:
這是我第一次形式加載集合的一個。
class TestType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('option', CollectionType::class, [
'entry_type' => SurveyOptionType::class,
'allow_add' => true,
'allow_delete' => true,
'entry_options' => [
'label' => false,
],
])
->add('submit', SubmitType::class, [
'label' => 'Salveaza',
])
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => SurveyManager::class
]);
}
}
這是集裝形式:
class SurveyOptionType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('isEnabled', CheckboxType::class, [
'label' => 'Chestionar Activ',
])
->add('headquarter', EntityType::class, [
'class' => HeadQuarterManager::class,
'multiple' => false,
'expanded' => false,
])
->add('userNumber', IntegerType::class, [
'attr' => [
'min' => '1',
'type' => 'number',
],
'label' => 'Numar Utilizatori',
])
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => SurveyOption::class
));
}
}
我的控制器動作:
public function newAction($surveyId, Request $request)
{
$surveyOption = new SurveyOption();
$em = $this->getDoctrine()->getManager();
$surveyRepository = $em->getRepository(SurveyManager::class);
$survey = $surveyRepository->findOneBy(['id' => $surveyId]);
$form = $this->createForm(TestType::class, $survey);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
}
return [
'surveyOption' => $surveyOption,
'form' => $form->createView(),
];
}
「SurveyManager」應該有一個'getOptions'方法,我認爲最好是renam e表單域爲'surveyOption',因此它永遠不會與表單選項衝突。回到Symfony 2.3中,另一種方法叫做「getOptions」,現在它被重命名爲「configureOptions」。 – Rvanlaak
實體具有所有必需的方法..因爲我使用了generatie:它們的實體 –