我尝试向会话var bu发送ajax请求添加多个值的数组。我这样做:
public function addFoodAction(Request $request)
{
$foodId = json_decode($request->getContent(), true)['food'];
$food = $this->getDoctrine()->getRepository('HackatonDinningRoomBundle:Food')->find($foodId);
$arr = array();
$arr[]=$food;
$session = $this->get('session');
$session->set('items', array($food));
return new Response(count($arr));
}
我怎么能这样做?
答案 0 :(得分:3)
使用serialize
:
$session->set('items', serialize(array($food)));
要检索数据:
unserialize($session->get('items'));