I am trying to store multiple values(products) in my session so that i can display them in a basket. However for some reason it only stores one product..
In the first pages controller I created an array in witch I will store my selected products later:
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$products = $em->getRepository('MpShopBundle:Product')->findAll();
$session = $this->getRequest()->getSession();
$item = array();
$session->set('itemCart', $item);
return $this->render('MpShopBundle:Frontend:index.html.twig', array(
'products'=>$products
));
}
Then when a user selects a product a new page opens with the products detail(product is selected by id). Then if the user adds the product to the cart he is taken to the basket page and I save the product in an array with session:
public function viewAction($id)
{
$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('MpShopBundle:Product')->find($id);
$session = $this->getRequest()->getSession();
$itemCart = $session->get('itemCart');
array_push($itemCart, $product);
$session->set('itemCart', $itemCart);
return $this->render('MpShopBundle:Frontend:product_details.html.twig', array(
'product'=>$product
));
}
In the basket page I get the products:
public function summaryAction()
{
$em = $this->getDoctrine()->getManager();
$products = $em->getRepository('MpShopBundle:Product')->findAll();
$session = $this->getRequest()->getSession();
$itemCart = $session->get('itemCart');
return $this->render('MpShopBundle:Frontend:product_summary.html.twig', array(
'products'=>$products,
));
}
Now in my twig I display them like this. First i get the products:{% set items = app.session.get('itemCart') %}
. Then i go cycle through them and try to display:
{% for item in items %}
<tr>
<td> <img width="60" src="{{ asset('bundles/mpFrontend/assets/products/4.jpg') }}" alt=""/></td>
<td>{{ item.model }}</td>
<td>
<div class="input-append"><input class="span1" style="max-width:34px" placeholder="1" id="appendedInputButtons" size="16" type="text"><button class="btn" type="button"><i class="icon-minus"></i></button><button class="btn" type="button"><i class="icon-plus"></i></button><button class="btn btn-danger" type="button"><i class="icon-remove icon-white"></i></button> </div>
</td>
<td>$120.00</td>
<td>$25.00</td>
<td>$15.00</td>
<td>$110.00</td>
</tr>
{% endfor %}
The first product is displayed. When I go back and select another product, the new product overlaps the older one and is the only one shown. Why?
PROBLEM FOUND. HOW To FIX IT?
Ok so I think I found my problem. Im only getting one value because when I add my first product to the basket I go back to my index page to select another product myy indexController $item = array();
part and makes my array empty again. Thats the problem. But if i select the first product and just browse back without reloading the page and add a new product it works. So what can I do with my indexController to make it not empty my cart everytime I go back?