Which properties (and why) should be included in serialize()
and deserialize()
methods in Symfony 2?
For now i've the id
field and it just works, but i'd like to know why and what's the purpose of serialize()
in User
class. in order to avoid this message:
You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine.
Class User implements AdvancedUserInterface, \Serializable
{
/**
* @return string
*/
public function serialize()
{
return serialize($this->id);
}
/**
* @param string $data
*/
public function unserialize($data)
{
$this->id = unserialize($data);
}
}
While without implementing \Serializable
and with all properties protected
, i'm getting:
Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::serialize() must return a string or NULL.
You need to serialize/deserialize the username and the fields you use in the equality check. You don't need to serialize the id
property unless it can be changed in your app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With