Symfony2 Error: UsernamePasswordToken::serialize() must return a string or NULL

Symfony2 Error: UsernamePasswordToken::serialize() must return a string or NULL

Getting the following error? Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::serialize() must return a string or NULL

I was getting it while trying to login a user. The thing was, in my Role entity, all properties were private.

  1. class Role implements RoleInterface
  2. {
  3.     private $id;
  4.    
  5.     private $name;
  6.    
  7.     private $created_at;
  8.    
  9.     // …
  10. }

When doing some googling and checking things out, I found this comment on php.net which gave me an idea. I changed all private properties to protected and thing worked!

  1. class Role implements RoleInterface
  2. {
  3.     protected $id;
  4.    
  5.     protected $name;
  6.    
  7.     protected $created_at;
  8.    
  9.     // …
  10. }

15 Responses so far.

  1. Michael Holm says:

    thanks a lot.. you made me able to progress my work on the project with upgraded versions of symfony :)

  2. drifter says:

    Thank You, that helped me!
    In my case, the problem was in one of my other entities, but related to User as well.

    By the way, I’m wondering, why entities fields are generated with ‘private’ keyword if that problem exists..

  3. wesley says:

    I have got the same problem :/

    but in my case it is because my User entity uses an relationship with Group. so the serialize crashes :/

    how can I fix it ?

  4. wesley says:

    please, reply in my email. thx.

  5. Metod says:

    I think that making all properties in User aswell as in Group entity protected should do the trick.

    Just don’t use the private access modifier.

  6. thorinkor says:

    You can also leave the properties private and add a serialization interface

    class Role implements RoleInterface, \Serializable
    {
    //…

    public function serialize()
    {
    return serialize(array(
    $this->id,
    $this->password,
    $this->username
    ));
    }

    public function unserialize($serialized)
    {
    list(
    $this->id,
    $this->password,
    $this->username
    ) = unserialize($serialized);
    }
    }

  7. thorinkor says:

    ^of course the fields in serialize and unserialize should be according to the class attributes (id, name).

    It’s worth to add that you need to serialize only the credentials used in authentication (like entity user: id, username, password).

  8. Le Barde says:

    Thank you very much !
    It worked once more :-)

  9. Salavert says:

    I modified all my entities to protected and did the trick, thanks a lot!!!

  10. Iman says:

    Metod and @thorinkor thanks the tips.

  11. Markhaen says:

    Great! Replacing all ‘private’ in my User entity worked. Strangely enough it worked before…

  12. Iulian says:

    Putting this in my user entity solved the problem:

    public function __sleep()
    {
    return array('id');
    }

  13. alami says:

    thanks it worked greatlly .