用Doctrine和MongoDB存储数组 [英] Storing array with Doctrine and MongoDB

查看:227
本文介绍了用Doctrine和MongoDB存储数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Doctrine和Mongo DB存储数组?

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

我不想要参考文档,只有数组。

I do not want reference document, only array.

示例:

Type[ 
     Type1,
     Type2,
     Type3
]

我需要创建新的Doctrine ODM数据类型吗?

Do I need to create new Doctrine ODM data type?

推荐答案

如果您需要存储未映射到数组中的文档类的值,则可以使用 collection 字段映射,它映射到MongoDB中的一个基本数组。还有一个 hash 类型,它类似地将PHP中的关联数组转换为MongoDB中的一个对象,而不对其中的任何内容进行映射。

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.

如果您的示例中的类型是映射的文档类,那么您将需要使用 EmbedMany 关系,它将一个或多个映射文档存储在父文档中的数组中。在MongoDB中,这将被表示为一个对象数组,它类似于您可以使用集合字段(存储关联数组的数组)然而,ODM将利用EmbedMany映射将这些对象恢复到文档实例。

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.

这篇关于用Doctrine和MongoDB存储数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆