CakePHP fixtures and database fields containing serialized data

I just came across and interesting issue while writing some CakePHP unit tests. I have a field that stores serialized data and the data is automatically unserialized via an afterFind() callback in the model.

When writing the test, I created a fixture and originally used run-time importing from the main database and all my tests worked fine. When I switched to prefabricated records written in the fixture file’s $records variable, I was getting errors with the unserialization.

Upon closer examination, I noticed that the bake script for fixtures was automatically escaping the double quotes in the serialized string, causing things to fail. So the point is, if creating fixture records via bake, be sure to remove any added slashes in fields containing serialized data.

Leave a Reply