我已经坚持了好几天,并尝试了我能在网上找到的所有解决方案,但没有运气。
我有一个学生链接到许多转移。每个转移都链接到一个类。
为了制作表格,我使用以下类:StudentType、TransferType。
StudentType 表单类有一个 TransferType 类型的字段。下面的代码。
学生类型.php
<?php
// src/PS/PSchoolBundle/Form/StudentType.php
namespace PS\PSchoolBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use PS\PSchoolBundle\Entity\Responsible;
use PS\PSchoolBundle\Entity\AuthorizedPerson;
use PS\PSchoolBundle\Entity\Transfer;
class StudentType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('subNumber', 'text', array('required' => false))
->add('subDate', 'date', array(
'required' => false,
'widget' => 'single_text',
'format' => 'dd/MM/yyyy',
'attr' => array('class' => 'datepicker')
))
->add('lastName', 'text')
->add('firstName', 'text')
->add('lastNameSecondLanguage', 'text', array('required' => false))
->add('firstNameSecondLanguage', 'text', array('required' => false))
->add('birthDate', 'date', array('required' => false,
'widget' => 'single_text',
'format' => 'dd/MM/yyyy',
'attr' => array('class' => 'datepicker')
))
->add('birthPlace', 'text', array('required' => false))
->add('birthPlaceSecondLanguage', 'text', array('required' => false))
->add('gender', 'choice', array('choices' => array('' => 'Veuillez choisir', 'm' => 'Garçon', 'f' => 'Fille')))
->add('nationality', 'text', array('required' => false))
->add('homeLanguage', 'text', array('required' => false))
->add('bloodType', 'text', array('required' => false))
->add('familySituation', 'choice', array('required' => false,
'choices' => array(
'' => 'Veuillez choisir',
'm' => 'Mariés',
'd' => 'Divorcés',
'w' => 'Veuve',
'a' => 'Autre')))
->add('schoolStatusBefore', 'choice', array('required' => false, 'choices' => array('' => 'Veuillez choisir', 's' => 'Scolarisé', 'ns' => 'Non scolarisé')))
->add('remark', 'textarea', array('required' => false))
->add('responsibles', 'collection', array('type' => new ResponsibleType,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true))
->add('authorizedPersons', 'collection', array('type' => new AuthorizedPersonType,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true))
->add('transfers', 'collection', array('type' => new TransferType,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true));
}
public function getName()
{
return 'student';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'PS\PSchoolBundle\Entity\Student'
);
}
}
传输类型.php
<?php
// src/PS/PSchoolBundle/Form/TransferType.php
namespace PS\PSchoolBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use PS\PSchoolBundle\Entity\Transfer;
use Doctrine\ORM\EntityRepository;
class TransferType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('reason', 'choice', array(
'choices' => array('success' => 'Réussite', 'failure' => 'Échec'),
'required' => false
))
->add('date', 'date', array(
'widget' => 'single_text',
'format' => 'dd/MM/yyyy',
'attr' => array('class' => 'datepicker')
))
->add('school', 'text', array('required' => false))
->add('classe', 'entity', array('class' => 'PSPSchoolBundle:Classe', 'property' => 'name'));
}
public function getName()
{
return 'transfer';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'PS\PSchoolBundle\Entity\Transfer'
);
}
}
当我将“classe”字段添加到 TransferType 时,我没有显示表单,而是抛出了这个错误:
必须管理传递给选择字段的实体 500 Internal Server Error - FormException
请注意,我已经尝试过 TransferType 并且表单正确显示并填充了类选择字段。只有当我同时使用 TransferType 和 StudentType 时才会出现这个问题。
谢谢
编辑 - 以下是生成表单的控制器的操作:
public function byIdEditAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
if(!$student = $em->getRepository('PSPSchoolBundle:Student')->find($id))
{
throw $this->createNotFoundException('Student[id='.$id.'] does not exist.');
}
$form = $this->createForm(new StudentType(), $student);
$formHandler = new StudentHandler($form, $this->get('request'), $em);
if($formHandler->process())
{
$this->get('session')->setFlash('wehappy', "L'élève a été mis à jour.");
return $this->redirect($this->generateUrl('_studentsbyidshow', array('id' => $student->getId())));
}
return $this->render('PSPSchoolBundle:Student:edit.html.twig', array(
'form' => $form->createView(),
'student' => $student
));
}