Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing array with Doctrine and MongoDB

How can I store an array with Doctrine and Mongo DB?

I do not want reference document, only array.

Example:

Type[ 
     Type1,
     Type2,
     Type3
]

Do I need to create new Doctrine ODM data type?

like image 337
user1754256 Avatar asked Oct 17 '12 19:10

user1754256


1 Answers

If you need to store values not mapped to a document class in an array, you can use the collection field mapping, which maps to a basic array in MongoDB. There is also a hash type, which similarly converts an associative array in PHP to an object in MongoDB without mapping anything within it.

If "Type" in your example is a mapped document class, then you'll want to use an EmbedMany relationship, which will store one or more mapped documents in an array within the parent document. Within MongoDB, this will be represented as an array of objects, which is similar to what you could do yourself with the collection field (storing an array of associative arrays); however, ODM will utilize the EmbedMany mapping to hydrate those objects back to document instances.

like image 169
jmikola Avatar answered Sep 17 '22 11:09

jmikola