Q:

Add array to session Symfony2

I try to add array of multiple vals to session var bu sending ajax request. I do this:
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));
}

How I can do that ?

A:

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

To retrieve the data:
unserialize($session->get('items'));

