/** * @Route("/Soda/{id}", name="soda_detail") */ public function sodaDetail($id, Request $request) { $soda = $this->getDoctrine()->getRepository('AppBundle:Soda')->find($id); //Setting up form $orderdetail = new Orderdetail(); $orderdetail->setSoda($soda); $form = $this->createForm('orderdetailsubmit', $orderdetail); $form->handleRequest($request); if ($form->isValid()) { //get user $user = $this->get('security.token_storage')->getToken()->getUser(); //adding order $order = new Order(); $order->setUser($user); //Relation setup $order->addOrderdetail($orderdetail); $order->setOrderdate(); $orderdetail->setOrder($order); //Persist $em = $this->getDoctrine()->getManager(); $em->persist($order); //Updating left sodas $oldStorage = $this->getDoctrine()->getRepository('AppBundle:Storage')->find($soda->getStorage()->getId()); $oldStorage->setQuantity($oldStorage->getQuantity() - $orderdetail->getAmount()); $em->merge($oldStorage); $em->flush(); return $this->redirectToRoute('soda_overview'); } return $this->render('default/detail.html.twig', array('soda' => $soda, 'form' => $form->createView())); }