drtkyykai004574380 2016-04-05 17:09
浏览 17
已采纳

如何清除购物车php [关闭]

I'm new to Symfony2 and PHP but I'm learning my ways.

I created a simple shopping cart. After they click 'Buy' I want the items in the cart to be cleared and effectively render that cart useless because a field would change from false to true in my database (which shows what carts are active and which ones aren't).

How come my code isn't doing anything I want it to do? The cart remains in-tact after I click 'Buy' (items are still in there) and in my DB my field doesn't change.

I would really appreciate some help with this, thanks.

/**
     * Displays the products bought from products 'added to cart'
     *
     * @Route("/bought", name="product_bought")
     * @Method("GET")
     * @Template()
     */
    public function boughtAction(Request $request) {

        $em = $this->getDoctrine()->getManager();

        $user = $this->getUser();

        $cart = $em->getRepository('ShopBundle:UserCart')->findOneBy(['user' => $this->getUser()]);

        $totalCostOfAllProducts = 0;

            $cart->getSubmitted(); //it's false
            var_dump($cart->getSubmitted());
            $cart->setSubmitted(true);
        $sub = $cart->getSubmitted(); //it's true

        if ($sub == true) {
            $cart = null;
        }
        // var_dump($cart);
        var_dump($sub);

        return array(
            'user' => $user,
            'quantity' => $cart->getQuantities(),
            'totalCostOfAllProducts' => $totalCostOfAllProducts,
        );
    }
  • 写回答

1条回答 默认 最新

  • duandan9680 2016-04-05 17:38
    关注

    it looks like you're not persisting and flushing the changes to the database. At the moment the value when you call ->getSubmitted() the second time will still be the same as when you called it the first time.

    For changes you have made to an entity to be updated you need to do the following:

    $em->persist($cart);
    

    this will mark the entity as having changes to it

    $em->flush();
    

    this will update all entities that have been persisted in your database and when you call ->getSubmitted() the second time it should be true.

    Adding these lines should fix the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么