Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which properties have to be serialized/deserialized in Symfony 2 User class?

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.

like image 707
gremo Avatar asked Jul 17 '12 18:07

gremo


1 Answers

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.

like image 195
Elnur Abdurrakhimov Avatar answered Nov 08 '22 09:11

Elnur Abdurrakhimov