Cannot serialize Symfony\Component\Cache\Adapter\AbstractAdapter

1k Views Asked by At

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)

2

There are 2 best solutions below

0
Chopchop On BEST ANSWER

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'];
    }
}
0
Stefan I On

Since I stumbled upon this thread while searching for a similar issue:

Make sure that your don't write any instances of Symfony\Component\Cache\Adapter\AbstractAdapter to PHPs' session.

On request end, php tries to serialize the session in order to pick it up on the next request. Symfony\Component\Cache\Adapter\AbstractAdapter throws an exception on serialization by design.

In our case, we had a Utility-class set as a property of an Object. The utility class held a reference to a Symfony FileCache. As soon as the Object was added to $_SESSION, the session-close failed with the exception shown above (since objects referenced utility, utility referenced the file-cache). Removing the reference made de-/serialization possible again.