actridge/Spot — Issue #7: Config can't be serialized
Author: TheSavior
Created: 2012-01-01 03:54:52+00:00
State: open

In my application I'm doing authentication against a db with Spot. Auth stores valid identities in a session variable. An identity is a simple object with two fields, a mapper instance, and an entity. It has two methods, a __get to be able to get any key, and a getIdentifier method which returns the primary key value of the entity.

Since I need the mapper to get the primaryKey field, the identity needs to store it. When saving the object in the session and the session is shut down, PHP serializes the object. So my Identity gets serialized, which serializes the mapper, which contains a Config object which is serialized, which contains adapters.

Here is where the problem arises. From what I've read online, PDO cannot be serialized.

This is the specific error I get: "Fatal error: Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances' in [no active file]:0 Stack trace: #0 [internal function]: PDO->__sleep() #1 {main} thrown in [no active file] on line 0"

After doing some research online, I came across a bunch of links, but this one in particular:
http://fabien.potencier.org/article/9/php-serialization-stack-traces-and-exceptions

So somehow a solution to this problem should be found.

In my particular case, I didn't need any of the information from the config to be serialized so I just told \Spot\Config to implement \Serializable

and added the two functions:

public function serialize()
{
return serialize(array());
}

public function unserialize($serialized)
{
}

Just telling it to not serialize the connections. The error no longer shows up. This is a solution that really only works for me though, as it is not the intended use of Config. I'm not sure what we should really do about it.