Reputation: 202
Faced with such a problem : when i click Add in the basket, I redirect to the basket_action
, but a product that I have added is not displayed. It's my action
/**
* @Route("/basket", name="basket_action")
* @param Request $request
* @return mixed
*/
public function basketAction(Request $request)
{
$session = $request->getSession();
if(!$session->has('basket_action'))
{
$session->set('basket_action', array());
}
$em = $this->getDoctrine()->getEntityManager();
$products = $em->getRepository('ModelBundle:Products')
->findArray(array_keys($session->get('basket_action')));
return $this->render("CoreBundle:Basket:basket.html.twig",
array(
'products' => $products,
'basket' => $session->get('basket')
));
}
What am i doing wrong. Please, help.
Upvotes: 0
Views: 72
Reputation: 2462
It is hart to give you any example, cause it looks like you missed some code or logic. From what I can see in
$session = $request->getSession();
if(!$session->has('basket_action'))
{
$session->set('basket_action', array());
}
you just try to analize, is 'basket_action' stored to session, if not then assign it with an empty array. But were do you suppose to set "basket_action" with real data?
so if you has it at some other action, pls show this code too, if no, then you should add setter to session with filled data for 'basket_action'
Update:
Ok, I see that seems you missed some logic at the code
It is still hard to guess what exactly you need, but If it is expected to add every new product by clicking "Add" and pickup all old added products from session, then it will looks like something:
/**
* @Route("/basket", name="basket_action")
* @param Request $request
* @return mixed
*/
public function basketAction(Request $request)
{
$session = $request->getSession();
$cartIds = $session->get('basket_action', array());
$cartIds[] = $request->query->get('id');
$session->set('basket_action', $cartIds);
$em = $this->getDoctrine()->getEntityManager();
$products = $em->getRepository('ModelBundle:Products')
->findArray($cartIds);
return $this->render("CoreBundle:Basket:basket.html.twig",
array(
'products' => $products,
));
}
P.S. sorry I can't know what you are suppose to get from $session->get('basket')", cause I don't see what did you store there, so I just deleted "'basket' => $session->get('basket')" from template params
Upvotes: 1