Sonntag, 18. Dezember 2011

Doctrine2/Symfony2 SimpleXML Serialization

I wasted a lot of time on this behaviour, so I'll give a short description of what happened and what was the solution.

Well, our setup contains symfony2 (2.0.4) as well as doctrine2. I store some data from an xml file into an entity using SimpleXML-Parser. This was working well, when suddenly in the dev environment I got the error, that the profiler could not serialize some doctrine2 entity because SimpleXML Elements cannot be serialized. As the error was in the Profiler, I was really confused about what happened.

Typecasting is the answer

The solution was quite easy: When storing data into an object, you have to typecast the SimpleXML stuff:
$object->SetName((string) $simpleXMLObject->name);

While doctrine2 has no problem with the typecast not beeing present, the moment you want to serialize the doctrine2 entity you run into that behaviour. I didn't get the reason why the profiler serializes the entity (and, what really confused me was that he does unserialize(serialize($entity));). I guess it is somehow needed, so stick with the typecast above and you won't have any problems.

What really bothers me is that this error cost me so much time, altough in retrospective it is a clear one. I guess because the changes triggering the error where done a few days before I recognized the error first lead to some misguided thoughts. So kids, always remember: Write tests :-)

Keine Kommentare:

Kommentar veröffentlichen