I'm having problems updating a form containing two entities ("Event" and "Info) in symfony. Everything works fine right up until the user tries to update the information. When they do it's all saved as new database entries.
public function editAction(Request $request, Event $event)
{
$user = $this->getUser();
$editForm = $this->createFormBuilder()
->setMethod('PUT');
$editForm->add("event", 'AppBundle\Form\EventType');
$editForm->add("info", 'AppBundle\Form\InfoType');
$editForm = $editForm->getForm();
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$formEvent = $editForm->get('event')->getData();
echo "Debug<br />";
echo "form id: ". $formEvent->getId(); // Wrong. Returns null.
echo "<br /> event id: ". $event->getId(); // The correct id of entity that should be updated.
$em->merge($formEvent);
$em->flush();
} else {
$editForm->get('event')->setData($event);
}
return $this->render('event/edit.html.twig', array(
'event' => $event,
'edit_form' => $editForm->createView()
));
I echo some debug information, and handleRequest() do map the new information into the event-entity found in $editForm->get('event') with the exception that it's Id is missing which I assume has something to do with the creation of the new entity instead of updating. What's up?
Routing:
event_edit:
path: /{id}/edit
defaults: { _controller: "AppBundle:Event:edit" }
methods: [GET, POST, PUT]
Twig:
{{ form_start(edit_form, {'attr': {'novalidate': 'novalidate'}}) }}
{{ form_errors(edit_form) }}
{{ form_widget(edit_form.event.title) }}
{{ form_widget(edit_form.info) }}
<input type="submit" value="Save" />
{{ form_end(edit_form) }}