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.

Leave a comment ?


  1. Thanks a lot !

  2. Many thanks, this was driving me nuts! A quick

    for f in $(find . -name “*.php”); do cp $f $f.bkp; sed -e ‘s/private \$/protected \$/g’ $f; rm $f.bkp; done

    did the trick for me then.

    Thanks again!

  3. Thanks a lot, that saved me a lot of time. Also thanks, Thomas, for the script for the fast fix.

    greets from Switzerland

  4. That’s perfect, thanks !

  5. claudson oliveira

    Thanks for this post. You save me.

  6. Gracias!!! :D

  7. Great! thank you a lot.
    You save me.

  8. Thank you very much! I’ve been more than 2 hours with this error, and finally it was a private property as you said… and I don’t know why I typed “private” in the first place.

  9. Thank you. You’ve saved me a lot of time.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>