private function processForm(Item $item) { $created = $item && null === $item->getId(); $statusCode = $created ? Response::HTTP_CREATED : Response::HTTP_NO_CONTENT; $method = $created ? "POST" : "PUT"; $form = $this->createForm(new ItemType(), $item, array('method' => $method)); $form->handleRequest($this->getRequest()); $view; if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($item); $em->flush(); $view = $this->view()->setStatusCode($statusCode); // HTTP Convention : return HTTP 201 Created + Location of created resource if ($created) { $view->setLocation($this->generateUrl('get_item', ['id' => $item->getId()])); } } else { $view = $this->view($form, Response::HTTP_BAD_REQUEST); } return $this->handleView($view); }
private function attachRandomTags(Item $item) { $tags_copy = $this->tags; $tags_to_attach = rand(0, sizeof($this->tags)); for ($i = 0; $i < $tags_to_attach; $i++) { $index = rand(0, sizeof($tags_copy) - 1); $item->addTag($tags_copy[$index]); array_splice($tags_copy, $index, 1); } }
/** * Add item * * @param \Interne\SeanceBundle\Entity\Item $item * * @return Meeting */ public function addItem(\Interne\SeanceBundle\Entity\Item $item) { $this->items[] = $item; $item->setPosition($this->getNewPosition()); $item->setMeeting($this); return $this; }