gremo
gremo

Reputation: 48459

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.

Upvotes: 6

Views: 3737

Answers (1)

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44841

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.

Upvotes: 4

Related Questions