即時嘗試在symfony2中製作一個簡單的購物車,但關於會話的文檔非常有限,我發現的唯一例子是關於用戶的。如何在symfony 2中管理簡單的購物車會話?
從我瞭解的看視頻是必須做出這樣的步驟:
1,確保有一個session數組,如果不申報新的會話陣列 變量session數組通過2個加array_push(); 3顯示會話
這是我到目前爲止的代碼:
public function sessiontestAction(Request $request)
{
$session = $request->getSession();
if(!$session)
{
$session->set('producto');
}
$em = $this->getDoctrine()->getManager();
$producto = $em->getRepository('savaInventarioBundle:TblProductos')->find(29);
if(!$producto){
throw $this->createNotFoundException('no se encontro el producto');
}
array_push($session, $producto);
return $this->render('savaInventarioBundle:Catalogo:sessiontest.html.twig',
array('productos'=> $session));
}
我的輸出只是我每次調用函數時拋出1級的產品,而不是一個以上,此外,它顯示了這個錯誤「警告:array_push( )期望參數1是陣列,給定對象i」的
有什麼區別? – xurshid29 2014-09-30 05:03:18
我問了3件事, 1-確保有一個會話數組,如果沒有聲明一個新的會話數組(你的答案缺少這個) 2通過array_push()添加變量到會話數組; 3-session session symfony以不同的方式管理會話,我不知道session-> has(),並想學習如何管理會話,我已經知道背後的邏輯我想要的,但不知道功能,必須弄清楚這些事情。 – 2one2 2014-10-01 00:39:35