Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Form Entity Update

Tags:

Can anyone please show me a specific example of a Symfony2 form entity update? The book only shows how to create a new entity. I need an example of how to update an existing entity where I initially pass the id of the entity on the query string.

I'm having trouble understanding how to access the form again in the code that checks for a post without re-creating the form.

And if I do recreate the form, it means I have to also query for the entity again, which doesn't seem to make much sense.

Here is what I currently have but it doesn't work because it overwrites the entity when the form gets posted.

public function updateAction($id) {     $em = $this->getDoctrine()->getEntityManager();     $testimonial = $em->getRepository('MyBundle:Testimonial')->find($id);     $form = $this->createForm(new TestimonialType(), $testimonial);      $request = $this->get('request');     if ($request->getMethod() == 'POST') {         $form->bindRequest($request);          echo $testimonial->getName();          if ($form->isValid()) {             // perform some action, such as save the object to the database             //$testimonial = $form->getData();             echo 'testimonial: ';             echo var_dump($testimonial);             $em->persist($testimonial);             $em->flush();              return $this->redirect($this->generateUrl('MyBundle_list_testimonials'));         }     }      return $this->render('MyBundle:Testimonial:update.html.twig', array(         'form' => $form->createView()     )); } 
like image 600
Jeremy Hicks Avatar asked Jul 07 '11 20:07

Jeremy Hicks


People also ask

How should be the process to add a new entity to the app in Symfony?

With the doctrine:database:create command we create a new database from the provided URL. With the make entity command, we create a new entity called City . The command creates two files: src/Entity/City. php and src/Repository/CityRepository.

What is entity Symfony?

Well, entity is a type of object that is used to hold data. Each instance of entity holds exactly one row of targeted database table. As for the directories, Symfony2 has some expectations where to find classes - that goes for entities as well.

What is doctrine repository?

A repository in a term used by many ORMs (Object Relational Mappers), doctrine is just one of these. It means the place where our data can be accessed from, a repository of data. This is to distinguish it from a database as a repository does not care how its data is stored.

What is Doctrine in Symfony?

Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.


2 Answers

Working now. Had to tweak a few things:

public function updateAction($id) {     $request = $this->get('request');      if (is_null($id)) {         $postData = $request->get('testimonial');         $id = $postData['id'];     }      $em = $this->getDoctrine()->getEntityManager();     $testimonial = $em->getRepository('MyBundle:Testimonial')->find($id);     $form = $this->createForm(new TestimonialType(), $testimonial);      if ($request->getMethod() == 'POST') {         $form->bindRequest($request);          if ($form->isValid()) {             // perform some action, such as save the object to the database             $em->flush();              return $this->redirect($this->generateUrl('MyBundle_list_testimonials'));         }     }      return $this->render('MyBundle:Testimonial:update.html.twig', array(         'form' => $form->createView()     )); } 
like image 150
Jeremy Hicks Avatar answered Oct 10 '22 23:10

Jeremy Hicks


This is actually a native function of Symfony 2 :

You can generate automatically a CRUD controller from the command line (via doctrine:generate:crud) and the reuse the generated code.

Documentation here : http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_crud.html

like image 36
adurieux Avatar answered Oct 10 '22 22:10

adurieux