我花了几个小时来解决我的问题,我希望在这里找到答案。
这是我的表单(ExperienceType)的代码:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('intitulePoste')
->add('entreprise')
->add('dateDebut')
->add('dateFin')
->add('mission')
->add('idContrat', 'entity', array(
'class' => 'ClasseBundle:Contrat',
'property' => 'contrat',
// 'property_path' => false,
'multiple' => false,
'mapped' => true,
'empty_value' => 'Contrat',
// 'expanded'=> true,
))
->add('idville', 'entity', array(
'class' => 'ClasseBundle:Ville',
'property' => 'nom',
// 'property_path' => false,
'multiple' => false,
'mapped' => true,
'attr' => array(
'class' => 'lesvilles'
),
'empty_value' => 'Ville',
))
;
}
这是负责在数据库中注册实体的方法:
public function testAction()
{
$exp = new Experience();
$form = $this->createForm(new ExperienceType(), $exp);
$request = $this->get('request');
if ($request->getMethod() == 'POST')
{
$form->bind($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$exp = $form->getData();
$exp->setIdMembre($this->idMembreAction());
$em->persist($exp);
$em->flush();
return $this->redirect($this->generateUrl('test'));
}
}
return $this->render('MembreBundle:Default:FormExp.html.twig', array(
'form' => $form->createView()));
}
当我验证我的表单时,我收到此错误:“ HTTP Error 500 (Internal Server Error) ”然后使用文件dev.log我阅读了此消息:
教义.DEBUG: INSERT INTO Experience (idContrat, idMembre, idVille, intitulePoste company, StartDate, EndDate,mission) VALUES {"1" (,,,,,,,????): "[object] (project \ \ ClasseBundle \ \ Entity \ \ Contract: {}) "," 2 ": 1," 3 ":" [object] (project \ \ ClasseBundle \ \ Entity \ \ City: {}) "," 4 "," ggg ","5":"华夫饼","6","2222/22/22","7","2222/22/22","8":"hjjhjhj"} []
因此,我在 ExperienceType 文件中添加了 idVille 和 idContart 字段的条件(必需为 false),并在 Experience.php 文件(实体)中添加了当我验证表单时两个字段都接受空值( nullable = true )我的实体毫无问题地存储在数据库中。所以有什么问题?谢谢