What properties should be serialized / deserialized in the Symfony 2 User class?

What properties (and why) should be included in the serialize() and deserialize() methods in Symfony 2?

Now I have an id field and it just works, but I would like to know why and what is the purpose of serialize() in the User class. to avoid this message:

You cannot upgrade a user from EntityUserProvider that does not contain an identifier. A custom object must be serialized using its own identifier displayed 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); } } 

Without implementing \Serializable and with all protected properties, I get:

Symfony \ Component \ Security \ Basic \ Authentication \ Token \ UsernamePasswordToken :: serialization () should return a string or NULL.

+6
source share
1 answer

You need to serialize / deserialize the username and fields that you use in equality checking . You do not need to serialize the id property if it cannot be changed in your application.

+4
source

Source: https://habr.com/ru/post/920722/


All Articles