Search code examples
mongodbsymfonydoctrinedoctrine-odm

How do I deserialize a Doctrine object with JMSSerializerBundle?


I am using Doctrine-ODM and I'm trying to (de)serialize document Objects in Symfony.

The serialization seems to work, but when I try to deserialize I get the following error:

You must define a type for My\Bundle\Repository\Item::$id.

I try to deserialize with this call:

$object = $serializer->deserialize($serializedItem, 'My\Bundle\Repository\Item', 'json');

My document has a few Embedded Documents but they seem to be okay in the serialized String. It is missing Information about the Document-Type. Here a simplified example of the output:

{
    id: "itemID",
    - embededDocument: {
        id: "embeddedDocumentID",
        moreInformation: true
    }
}

now I have read the usage instructions and I assume that my solution is explained in the metadata for third party cookbook, but I can't figure out what to do.

How do I fix that and deserialize my Strings to Objects properly?


Solution

  • In My\Bundle\Repository\Item, you need to define an id property, and decorate it with the @Type annotation.