如何清除购物车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,
        );
    }

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.