I have a Service class that stores an object into the session
$this->session->set('utmUser', $this->session->serialize($this->utmUser));
And I get the unserialized object from the session:
$this->utmUser = $this->session->unserialize($this->session->get('utmUser'));
But the result is not an instance of the stored object.
The session contains this serialized string:
string(221) “a:2:{i:0;O:68:”Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage”:1:{s:10:”*options”;a:5:{s:8:”lifetime”;i:0;s:4:”path”;s:1:”/”;s:6:”domain”;s:0:””;s:6:”secure”;b:0;s:8:”httponly”;b:0;}}i:1;s:2:”en”;}”
Setting all properties of the object as protected doesn’t work.
What am I doing wrong?
session->serialize() and unserialize() do not do what you think they do, they do not serialize and unserialize an object, they serialize and unserialize the whole session. They do not take any parameters and in fact have been removed in Symfony 2.1
You can just set the object into the session and Symfony will take care of serializing and unserializing it for you, just use:
and
In any case you may reconsider if you really need to store the whole object into the session. If this is a big object this can be very inefficient. If that is the case and the object is stored in a database, you can store just the id of the object and then reload it from the id when you need it.