I am testing the Form and Doctrine2 functionality of Symfony2 and I love it however, the documentation on the Symfony2 website is not entirely clear (to me) concerning the updating of an Entity.
I have created a simple form for adding and deleting users. My controller action looks like follows: /** * @Route("/user/{id}", name="dashboard_useroverview", defaults={"id" = null}, requirements={"id" = "\d+"}) * @Template() */ public function userOverviewAction($id) { $em = $this->get('doctrine')->getEntityManager(); if(null === $id) { $user = new \Acme\DemoBundle\Entity\User(); } else { $user = $em->find('Acme:DemoBundle:User', $id); } $form = $this->getUserForm($user); $users = $em->getRepository('Acme:DemoBundle:User')->findAll(); return array('user' => $user, 'users' => $users, 'form' => $form->createView()); } As you can see I create an empty entity and bind it to the form when no ID is given (you can edit the user by passing the ID). When you press the submit button you go to the following action: /** * @Route("/user/submit", name="dashboard_usersubmit") */ public function userSubmitAction() { $request = $this->get('request'); $em = $this->get('doctrine')->getEntityManager(); $postData = $request->get('form'); $newUser = true; if($postData['id'] === '') { $user = new \Acme\DemoBundle\Entity\User(); } else { $user = $em->find('Acme:DemoBundle:User', $postData['id']); $newUser = false; } $form = $this->getUserForm($user); if ($request->getMethod() == 'POST') { $form->bindRequest($request); if($form->isValid()) { $user = $form->getData(); if($newUser) { $em->persist($user); } $em->flush(); } } return $this->redirect($this- >generateUrl('dashboard_useroverview')); } However, I have the feeling this is a bit 'nasty'. Since I don't have to worry about the post-fields generated by the Form component etc. I have to actively query the Request object to check whether or not an ID field is passed or not. I need the User object from the Entity Manager or else Doctrine won't update it and just generate a new one. Somehow I am missing something. How can I check if it is a new User or an updated one and work in a clean object-oriented way, because I don't think I have to query the Request object for this. -- If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com You received this message because you are subscribed to the Google Groups "symfony users" group. To post to this group, send email to symfony-users@googlegroups.com To unsubscribe from this group, send email to symfony-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/symfony-users?hl=en