symfonyfosuserbundle

Cannot serialize Symfony\Component\Cache\Adapter\AbstractAdapter


I removed FosUserBundle and develop my own User module. Since then i have this error that popsup when i try to serialize the session.

$session->set($this->sessionKey, serialize($token));

EDIT 1 : I posted this question even if I have the answer, since I spent 3 days on this problem and it can help someone else (my future me passing by for instance)

EDIT 2 : since I once again had this issue (without FosUserBundle) I thanks "Stefan I" to have taken time to explained his experience)


Solution

  • The problem was the User entity wasn't correctly seralized in session. I had to modify my entity as follow.

    If you don't serialize your User, the entire object will be serialied in _security_main (or _security_yourfirewall) variable session. For me it was more than 100 000 char length.

    class User implements UserInterface ,\Serializable
    {
        /**
         * {@inheritdoc}
         */
        public function serialize()
        {
            return serialize([
                $this->password,
                $this->salt,
                $this->username,
                $this->enabled,
                $this->id,
                $this->email,
                $this->roles,
                $this->groups
            ]);
        }
    
        /**
         * {@inheritdoc}
         */
        public function unserialize($serialized)
        {
            $data = unserialize($serialized);
    
            list(
                $this->password,
                $this->salt,
                $this->username,
                $this->enabled,
                $this->id,
                $this->email,
                $this->roles,
                $this->groups
                ) = $data;
        }
    }
    

    I had to update my serialize function since Symfony 6.3

    class User implements UserInterface,\Serializable,...
    {
    ...
    public function serialize()
        {
            return serialize([
                'id' => $this->getId(),
                'password' => $this->getPassword(),
                'email' => $this->getEmail(),
                'userIdentifier' => $this->getEmail(),
                'username' => $this->getUsername(),
                'salt' => $this->getSalt(),
                'roles' => $this->getRoles(),
                'enabled' => $this->isEnabled(),
            ]);
        }
    
        public function unserialize($data)
        {
            $unserialized = \unserialize($data);
    
            $this->id = $unserialized['id'];
            $this->email = $unserialized['email'];
            $this->username = $unserialized['username'];
            $this->password = $unserialized['password'];
            $this->salt = $unserialized['salt'];
            $this->enabled = $unserialized['enabled'];
        }
    }