5

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

Posted December 5th, 2011 in Razno by Metod

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. }

5 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.