I am trying to compose a very basic form which is capable of inserting and as well updating records in the database. My problem is that each time I press the Save button, a new row is created in the DB. Could you please point to the problem in my source code?
controller: http://pastebin.com/YjMAdHqp
routing: http://pastebin.com/WSV6xCSw
The id column in the database is an auto-increment integer.
Thanks for any ideas.
The right answer for your question is using merge method instead persist. eg:
$your_entity = $form->getData();
$this->em->merge($your_entity); // MERGE not PERSIST
$this->em->flush();
You can read more about Merge Method at the official documentation
Cheers,
if ($id == 0) {
$task = new Task();
}
Your ID parameter defaults to 0 and a new Task is created instead of updating an existing tast.
hello_index:
pattern: /hello/index/{id}
defaults: { _controller: AcmeHelloBundle:hello:index, id:0 }
You should seperate create and edit/update action and create a routing for each action.
http://symfony.com/doc/2.0/book/doctrine.html#updating-an-object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.