Esempio n. 1
0
 /**
  * @Route("/admin/stock/{id}/edit")
  */
 public function updateStock(Request $request, $id)
 {
     $entityManager = $this->getDoctrine()->getManager();
     $soda = $this->getDoctrine()->getRepository('AppBundle:Soda')->find($id);
     if (!$soda) {
         throw $this->createNotFoundException('No soda found');
     }
     $stock = $this->getDoctrine()->getRepository('AppBundle:Stock')->find($soda->getStock()->getId());
     if ($stock === null) {
         $stock = new Stock();
     }
     $isSodaInStock = $stock->getSoda($soda->getid());
     if (!$isSodaInStock) {
         $stock->addSoda($soda);
         $entityManager->persist($stock);
         $entityManager->flush();
     }
     $form = $this->createForm(new StockType(), $stock, array('method' => 'POST'));
     $form->add('save', 'submit', array('label' => 'Update Stock for ' . $soda->getName()));
     $form->handleRequest($request);
     if ($form->isValid()) {
         $entityManager->persist($stock);
         $entityManager->flush();
         return $this->redirectToRoute('admin_overview');
     }
     return $this->render('admin/Stock.html.twig', array('form' => $form->createView()));
 }