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

查看:25
本文介绍了使用 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 中,这将表示为一个对象数组,这类似于您可以自己使用 collection 字段(存储关联数组的数组);但是,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天全站免登陆