Пример #1
0
 /**
  * @Route("/add/{type}", name="add_item")
  */
 public function addItemAction($type, Request $request)
 {
     $repository = $this->getDoctrine()->getRepository('AppBundle:Item');
     if (!$repository->setType($type)) {
         throw new \AppBundle\Exception\InvalidItemTypeException($type);
     }
     switch ($type) {
         case "restaurant":
             $item = new \AppBundle\Entity\Items\Restaurant();
         case "doctor":
             $item = new \AppBundle\Entity\Items\Doctor();
     }
     $tag = new Tag();
     $tag->setContent("Test");
     //         $item->addTag($tag);
     $form = $this->createForm(ItemType::class, $item);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         $item->setCreationDate(new \DateTime('now'));
         $item->setModificationDate(new \DateTime('now'));
         $item->setLocation(new Point($form->get('longitude')->getData(), $form->get('latitude')->getData()));
         //FIXME: This is a workaround, mapping should be automatical
         foreach ($form->get('tags') as $tag) {
             $t = new Tag();
             $t->setType($tag->get('type')->getData());
             $t->setContent($tag->get('content')->getData());
             $t->setItem($item);
             $item->addTag($t);
         }
         $em = $this->getDoctrine()->getManager();
         $em->persist($item);
         $em->flush();
         return $this->redirectToRoute('display', array("id" => $item->getId()));
         /*
         return $this->render('add/' . $type . '.html.twig', [
             'form' => $form->createView(),
             'item' => $item,
         ]);
         */
     } else {
         return $this->render('add/' . $type . '.html.twig', ['form' => $form->createView()]);
     }
 }