Saturday, November 26, 2011

Mapping embedded documents in Doctrine MongoDB ODM

If you have a MongoDB document that has another document embedded in it, you still have to create a separate XML mapping document for the embedded one. So if you have a parent document called Activity that has an embedded document called Coordinates, the configuration would look something like this:


<doctrine-mongo-mapping xmlns=""

    <document name="Acme\ActivityBundle\Document\Activity" db="acme" collection="activity" customId="true">
        <field fieldName="id" id="true" strategy="INCREMENT" />

        <embed-one target-document="Acme\ActivityBundle\Document\Coordinates" field="locationCoordinates" />

            <lifecycle-callback method="callbackPrePersist" type="prePersist" />
            <lifecycle-callback method="callbackPreUpdate" type="preUpdate" />
                <key name="locationCoordinates" order="2d" />


<doctrine-mongo-mapping xmlns=""

    <embedded-document name="Acme\ActivityBundle\Document\Coordinates">
        <field fieldName="latitude" type="float" />
        <field fieldName="longitude" type="float" />

The XML mapping format is not included in the Doctrine MongoDB ODM documentation. After a bunch of tests, I realized the @EmbeddedDocument in the annotation format example and simply replaced the document tag with embedded-document in the mapping file.

No comments:

Post a Comment