Sonntag, 25. November 2012

Doctrine MongoDB ODM: Storing arrays

When working with Doctrine MongoDB ODM, one thing I struggled to get right was arrays inside a document. I remember a few questions on StackOverflow who also didn't know how to handle this.

The answer is very trivial, but the documentation lacks a description, so I am posting it here (as well as adding it to the documentation of course):

class MyDocument
{
    /**
     * @Field(type="hash")
     */
    private myArray = array();
}


There is a Mapping Type called "hash" which mappes a PHP array to a MongoDB one.

Of course you need to remember that an array in a MongoDB document cannot have any keys but only values. If you need keys, that would be a new document which gets embedded (see the docs on embedded documents).

Keine Kommentare:

Kommentar veröffentlichen