0

I working on bookstore project, I have a list of books with button Add to Cart , click on that should save the ID of book in session by some name. But problem for me is how can save many this ID inside session so I can later access them inside foreach?

/**
 * @Route("/cart/{bookId}", name="AppBundle_Book_addingToCartAction")
 */
public function addingToCartAction(Request $request, int $bookId)
{
    // getting session 
    $sessionCart = $request->getSession();

    // when user click on button Add to Cart , i send ID of book here(and others book IDs),
    // so i need to save that ID inside a session(i was thinking making of some array
    // with all this values), so how can I do this,i trying like this:
    $sessionCart->set('BookIDs', array('Book'.$bookId => $bookId));

    return $this->render('AppBundle:Books:shopingCart.html.twig', array(
        'id' => $bookId,
    ));
}
3
  • with that i actually just exchange bookId with another bookId. Commented Jan 25, 2017 at 14:57
  • So in other words you want an array of BookIds? Is that correct? And later you'll want to iterate over that array and process them - Is this a correct presumption? Just trying to understand the problem... Commented Jan 25, 2017 at 16:44
  • yeah, i got the solution, but forget to checked the answer. Thank you for replay Commented Jan 25, 2017 at 17:47

2 Answers 2

1

The simplest solution would be this I think.

1) create a key in the session object for your cart, then you can segregate it from other things in the session.

2) get and set the contents of that in the same way you would any other array.

3) Just keep the bookId in there unless you have some need for anything else.

/**
 * @Route("/cart/{bookId}", name="AppBundle_Book_addingToCartAction")
 */
public function addingToCartAction(Request $request, int $bookId)
{
    // getting session 
    $sessionCart = $request->getSession();

    $cart = $sessionCart->get('cart');

    $cart[] = $bookId;

    $sessionCart->set('cart', $cart);

    return $this->render('AppBundle:Books:shopingCart.html.twig', array('id' => $bookId));
}
Sign up to request clarification or add additional context in comments.

2 Comments

where should i create a key('cart') for session, you using --- $cart = $sessionCart->get('cart');
When you first add something to the cart in the session
0

you should get the array from the session variable. And add the new book Id in this array. Otherwise you will override this variable.

$arrayOfBook = $sessionCart->get('BookIDs');
if ($arrayOfBook == null){
  arrayOfBook = [];
}
arrayOfBook[] = $bookId;
$sessionCart->set('BookIDs', arrayOfBook);

2 Comments

yes, this have sence, but where should i create a session key, i cannot creat inside that action
tnx, it helped me alot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.