Reputation: 3690
I have a form where the user enters data. Then, when they click submit, this data will be saved to the database.
The only thing is, it doesn't do this. What does happen, is that when submit is clicked, the page reloads and all the data entered in the form will still be displayed. I go to check the database, and the records haven't been updated.
There are no errors, but according to the profiler there are three SQL statements run when the page is loaded. All three of these are SELECT statements, not one INSERT statement there.
Here is the code for the page in the controller (including the "INSERT" statement):
public function addAction(Request $request)
{
$pageadd = new Content();
$form = $this->createForm(new PageAdd(), $pageadd);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($pageadd);
$em->flush();
return new Response('Created page id '.$pageadd->getId());
}
}
return $this->render('ShoutAdminBundle:Default:pageadd.html.twig', array(
'form' => $form->createView()
));
}
Here is the code for the form (I've omitted some of the fields for space reasons. But they are all identical):
<form action="{{ path('ShoutAdminBundle_adminpageaddpost') }}" method="post" {{ form_enctype(form) }} class="blogger">
<p class="row">
{{ form_label(form.id, 'ID*', { 'attr': {'class': 'title'} }) }}
{{ form_errors(form.id) }}
{{ form_widget(form.id, { 'attr': {'class': 'textfield'}}) }}
</p>
<p class="row">
{{ form_label(form.title, 'Title*', { 'attr': {'class': 'title'} }) }}
{{ form_errors(form.title) }}
{{ form_widget(form.title, { 'attr': {'class': 'textfield'}}) }}
</p>
<p class="row">
<input type="submit" value="Save This Page" class="savebutton" />
</p>
</form>
If you need any more code I will provide them. I think these two bits of code is where the problem lies though.
Cheers!
Upvotes: 2
Views: 3088
Reputation: 111
You must fill the entity before the persist, I give you an example:
public function saveAction(Request $request)
{
if ($request->getMethod() == 'POST') {
// EntityManager
$em = $this->getDoctrine()->getEntityManager();
// New entity
$registration = new Customer();
// Build the form
$form = $this->createFormBuilder()
->add('name', 'text')
->add('country', 'text')
->add('email', 'email')
->add('certificate', 'text')
->add('provider', 'entity', array(
'class' => 'YourCustomBundle:Partner',
))
->getForm();
// Populate
$form->bindRequest($request);
// Check
if($form->isValid()) {
// Fill the entity
$registration->setName($form['name']->getData());
$registration->setCountry($form['country']->getData());
$registration->setEmail($form['email']->getData());
$registration->setCertificate($form['certificate']->getData());
$registration->setProvider($form['provider']->getData());
$em->persist($registration);
$em->flush();
}
}
return $this->render('YourCustomBundle:Default:index.html.twig',array(
'form' => $form->createView(),
));
}
Upvotes: 2