Reputation: 3356
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.
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.
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?
Upvotes: 3
Views: 1017
Reputation: 57709
Since
$products = $session->get('products');
To set it back correctly, use:
$session->set('products', $products);
Upvotes: 1