I am designing a Shopping Cart. This is my payment action in the controller.
public function paymentAction()
{
$em = $this->getDoctrine()->getManager();
$securityContext = $this->container->get('security.context');
$userid=$securityContext->getToken()->getUser()->getId();
$total = $em->getRepository('ShopperShopBundle:Item')->getTotal1($userid);//get total price
// var_dump($total);exit;
//update order table
$order = new Order();
$order->setUser1($em->getReference('ShopperShopBundle:User', $userid))
->setTprice($total[0][1])
->setStatus('Incomplete');
//var_dump($order);exit;
$em->persist($order);
//empty the cart
$remove = $em->getRepository('ShopperShopBundle:Item')->findBy(array('user'=>$userid));
if (!$remove) {
throw $this->createNotFoundException(
'Your Cart is empty. If you checked out recently and revisiting the cart, the items are lost. You can go to orders and complete your incomplete transaction. :-('
);
}
$em->remove($remove[0]);
$em->flush();
return $this->render('ShopperShopBundle:Default:payment.html.twig',array('categories'=>0,'total'=>$total[0][1]));
}
It inserts into Order table twice. But when I exit before returning, order is inserted only once.
These are my routes.
shopper_shop_checkout:
pattern: /checkout
defaults: { _controller: ShopperShopBundle:Default:checkout }
shopper_shop_payment:
pattern: /payment
defaults: { _controller: ShopperShopBundle:Default:payment }
If you need more information please ask in comments. Problem occurs while rendering the page. Somehow, the insertion query is called again.