/** * Update existing post from the submitted data or create a new post at a specific location. * * @ApiDoc( * resource = true, * input = "AppBundle\Entity\Post", * statusCodes = { * 201 = "Returned when a new resource is created", * 204 = "Returned when successful", * 400 = "Returned when the form has errors" * } * ) * * @Put("/posts/{id}", requirements={"id" = "\d+"}) * * @param Request $request * @param int $id * * @return View */ public function updatePostAction(Request $request, $id) { $em = $this->getDoctrine()->getManager(); $post = $em->getRepository('AppBundle:Post')->find($id); if (null === $post) { $post = new PostEntity(); $post->setId($id); $em->persist($post); $metadata = $em->getClassMetadata(get_class($post)); $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE); $metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator()); $statusCode = Response::HTTP_CREATED; } else { $statusCode = Response::HTTP_NO_CONTENT; } $form = $this->createForm(new PostRestType(), $post); $form->submit($request); if ($form->isValid()) { $post->setSlug($this->get('slugger')->slugify($post->getTitle())); $em->flush(); return $this->routeRedirectView('get_post', array('id' => $post->getId()), $statusCode); } $view = new View($form); return $this->handleView($view); }