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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题