我已经坚持了几天,并尝试了我可以在网上找到的所有解决方案,但没有运气 .
我有一个学生链接到许多转移 . 每次转移都与Classe相关联 .
要创建表单,我使用以下类:StudentType,TransferType .
StudentType表单classe有一个TransferType类型的字段 . 代码如下 .
StudentType.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'
);
}
}
TransferType.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内部服务器错误 - FormException
请注意,我已经尝试过TransferType,并且填写了classe选择字段时表单正确显示 . 只有当我一起使用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
));
}
1 回答
我的问题实际上与我使用具有实体字段(Transfer)的StudentType这一事实有关,该实体字段本身具有实体字段(Classe) . 链接到Student对象的传输对象没有链接到它的Classe,因此我抛出了异常,抱怨需要添加到Classes选项字段的Classe对象未被管理 . 这意味着它必须由实体经理管理 . 换句话说,它必须来自数据库 .
有两种方法可以解决这个问题:显而易见的一种方法是修复数据库中缺少的链接,因为该关系在Doctrine上设置为不可为空 . 第二种方法是创建一个默认的Classe对象并从数据库中提取它,然后将其分配给传输,如下所示:
在使用StudentType实际创建表单之前...
希望这有助于某人 .