Reputation: 651
I spent hours to find a solution to my problem, and I hope to find the answer here.
Here is the code for my form(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',
))
;
}
And here is the method that is responsible for registering the entity in the database :
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()));
}
When I validate my form I get this error: "HTTP Error 500 (Internal Server Error)" then using the file dev.log I read this message:
doctrine.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 ":" waffle "," 6 "," 2222/22/22 "," 7 "," 2222/22/22 "," 8 ":" hjjhjhj "} []
So I added in the ExperienceType file a condition on both idVille and idContart fields (required false) and I added in the Experience.php file (entity) that both fields accept the null values (nullable = true) when I validate my form my entity is stored in the database with no problem. So what's the problem? thanks
Upvotes: 1
Views: 215
Reputation: 9374
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;
Upvotes: 1
Reputation: 6410
You can remove the line $exp = $form->getData();
because it returns the data as an array
and not as an object. Because you passed the $exp
variable to the createForm()
method your entity is automatically mapped to your form.
Upvotes: 0
Reputation: 460
byf-ferdy's suggestion of removing the $exp = $form->getData();
looks correct.
For others with similar issues, http://symfony.com/doc/2.1/book/forms.html#forms-and-doctrine has more information on this.
Upvotes: 0