1

我花了几个小时来解决我的问题,我希望在这里找到答案。

这是我的表单(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 )我的实体毫无问题地存储在数据库中。所以有什么问题?谢谢

4

3 回答 3

1

You probably mapped the objects in wrong way. I see in you form field named idContrat, which suggest you try to store id property of object Contract. That's why you needed __toString() method in that object.

You mapping should look more like that:

/**
 * @ORM\ManyToOne("You\SomeBundle\Entity\Contract")
 * @ORM\JoinColumn(name="contract_id", referencedColumnName="id")
 * /
protected $contract;

than:

/**
 * @ORM\Column(name="idContract", type="integer")
 * /
protected $contract_id;
于 2013-07-19T06:53:50.360 回答
0

byf-ferdy删除$exp = $form->getData();外观的建议是正确的。

对于其他有类似问题的人,http://symfony.com/doc/2.1/book/forms.html#forms-and-doctrine对此有更多信息。

于 2013-06-15T16:23:18.420 回答
0

您可以删除该行$exp = $form->getData();,因为它将数据作为 anarray而不是作为对象返回。因为您将$exp变量传递给createForm()方法,所以您的实体会自动映射到您的表单。

于 2013-06-18T08:53:03.227 回答