Which mapping type to choose for associative Arrays? Doctrine ODM

前端 未结 5 2292
深忆病人
深忆病人 2021-02-13 23:45

I have a simple question about the (by the way really great!) Doctrine ODM.

Assume you have a document like:

/**
 * @Document
 */
class Test
{
    /** @I         


        
5条回答
  •  滥情空心
    2021-02-14 00:22

    The best answer is using hash type. But if for some reason you wantn't use hash type, you can use EmbeddedDocument feature provided by Doctrine ODM like the documentation says:

    If you are using the hash type, values within the associative array are passed to MongoDB directly, without being prepared. Only formats suitable for the Mongo driver should be used. If your hash contains values which are not suitable you should either use an embedded document or use formats provided by the MongoDB driver (e.g. \MongoDate instead of \DateTime).

    So, you need to create EmbeddedDocument EmbeddedExample in AppBundle\Document\EmbeddedExample.php:

    Then, you can use EmbeddedExample in your Test document. So the Test.php file will be similar to this:

提交回复
热议问题