3

I have an array called $products and I am assigng that to symfony session.

ARRAY

Array
(
    [services] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [icon] => bus.png
                    [name] => Web Development
                    [cost] => 500
                )

            [1] => Array
                (
                    [id] => 2
                    [icon] => ok-shield.png
                    [name] => Saadia
                    [cost] => 200
                )

            [2] => Array
                (
                    [id] => 3
                    [icon] => car.png
                    [name] => Web Development 2
                    [cost] => 200
                )

        )

)

This is how I am assigning it to session

$session = $request->getSession();
$session->set('products', array('services' => $products));

Now, if I access this session in twig using {{ dump(app.session.get('products')) }} I can view and access it perfectly fine.

enter image description here

These session values are actually used in a shopping cart that has a delete link next to each product and if you click on the delete link that specific product should get removed from session array so to achieve this I am using a delete route that calls the following function to remove that part of the array and reassign updated array to same session variable

public function CartRemoveAction($id, Request $request){
    $session = $request->getSession();
    $products = $session->get('products');
    foreach($products['services'] as $key => $service)
    {
        if($service['id'] == $id)
        {
            unset($products['services'][$key]);
            break;
        }
    }
    $session->set('products', array(
        'services' => $products,
    ));
    return $this->render('featureBundle:Default:cart.html.twig');
}

But this leaves me with 2 services array in twig.

enter image description here

Even if I remove the session and reassign it, I end up with the same problem. I am using the following code to remove products array from session. $session->remove('products');

What am I doing wrong which is leading to 2 arrays or services?

4
  • Is there a reason you can't do $session->set('products, $products)? Or even $session->set('products', array("services" => $products["services"])); Commented Jan 18, 2015 at 15:15
  • i just tried $session->set('products, $products) and this fixed the problem, was stuck at this for hourssss. Would you like to add this as an answer so I can accept it. Commented Jan 18, 2015 at 15:19
  • Sorry to hear that, maybe take a break, make some tea, go for a walk. Commented Jan 18, 2015 at 15:26
  • you are correct, i need to take a break. Many thanks Commented Jan 18, 2015 at 15:27

1 Answer 1

1

Since

$products = $session->get('products');

To set it back correctly, use:

$session->set('products', $products);
Sign up to request clarification or add additional context in comments.

Comments

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.