Monthly Archives: September 2011

Symfony2 – “id” returned as member variable from __sleep() but does not exist

When storing entity objects in the http session, you might run into following exception :

ErrorException: Notice: serialize() [function.serialize]: “id” returned as member variable from __sleep() but does not exist in…

Now, apparently php has some problems serializing an object which subclasses an object with private properties. As Doctrine always creates subclasses of your entity classes (which are called Proxy classes), this means you should never use private properties (e.g. $id or any other property) on your entity (or associated entities) ! Use protected instead.

Custom validation messages for standard validators in Symfony2

Symfony2 contains a lot of validators out of the box, but it is not very clearly documented how to use custom messages when validation fails for these standard validators. After some investigation, I found the validation messages in Symfony/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/translations/. It is very easy to override a message. Just look up the message you wish to modify (e.g. ‘This value should not be blank’), create your own validators.[language].yml file (or .xliff if you prefer) in your bundle, and add this message as a key with your custom message as value :

# validators.en.yml

This value should not be blank: This is a required field

That’s all :)