将数组添加到会话Symfony2

时间:2014-12-14 05:26:02

标签: php ajax symfony session

我尝试向会话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));
}

我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

使用serialize

$session->set('items', serialize(array($food)));

要检索数据:

unserialize($session->get('items'));