0

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 ?

1
  • I see some issues with your code but what are you expecting and what are you actually receiving? Commented Dec 14, 2014 at 5:53

1 Answer 1

3

Use serialize:

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

To retrieve the data:

unserialize($session->get('items'));
Sign up to request clarification or add additional context in comments.

2 Comments

In addition to what @Gnucki said, I'd recommend using something other than the default serializer in php. Things like circular references can cause your program to crash. I'd recommend jms/serializer (but symfony/serializer works also).
echochamber is right, you should take a look at JMSSerializer if the serialized object is a bit complicated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.