If you're using a DQL Query: try { $em->createQuery('...')->getSingleResult(); } catch(\Exception $e) { return new Response('...'); }
Will throw an exception if no record is returned. Same thing with "getResult" instead of "getSingleResult" should return an ArrayCollection with a count of 0 if nothing is found: $users = $em->createQuery('...')->getResult(); if($users->count() == 0) throw new Exception('...'); (return a Response, throw an Exception, whatever you like, but those are the tests I currently use). Roger On Jun 2, 10:03 am, thesaint <gabriel.bi...@gmail.com> wrote: > In your controllers you'll often load entities with IDs coming from > the URL. What's the recommended way to deal with the case that an > entity was not found? > > In Symfony 1.4 it was > > $id = $request->getParameter('id); > $this->forward404Unless($user = Doctrine_Core::getTable('User')- > > >find($id), sprintf('User not found (%s).', $id)); > > For the time being I do the following in SF2: > > $em = $this->getEntityManager(); > $user = $em->find('MyBundle:User', $id); > if(!$user) { > throw new Symfony\Component\HttpKernel\Exception > \NotFoundHttpException(sprintf('User not found (%s).', $id)); > > } > > Are there better/recommended ways? -- 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