为关联数组选择哪种映射类型?学说ODM
我有一个关于 Doctrine ODM 的简单问题(顺便说一下,真的很棒!)。
假设您有一个如下文档:
/**
* @Document
*/
class Test
{
/** @Id */
public $id;
/** @WHICHTYPE */
public $field = array();
}
现在我想存储一个关联数组,例如
array("test" => "test1", "anothertest" => "test2", ......);
在该类的 $field 属性中。
我知道,对于 MongoDB 来说没问题,但在 Doctrine 中,当我使用 @Collection 或简单的 @Field 时,仅存储值(例如,在映射驱动程序中使用 array_values 进行集合)。所以存储的值看起来像
array("test1", "test2", ....)
有谁知道我应该使用哪种 Doctrine-ODM 映射类型来保留数据库中的键值对?
提前谢谢你,
安迪(来自德国的问候)
I have a simple question about the (by the way really great!) Doctrine ODM.
Assume you have a document like:
/**
* @Document
*/
class Test
{
/** @Id */
public $id;
/** @WHICHTYPE */
public $field = array();
}
Now i want to store an associative array like
array("test" => "test1", "anothertest" => "test2", ......);
In the $field property of that class.
No problem for MongoDB, I know, but in Doctrine when I use for example @Collection or simply @Field, only the values are stored (array_values is being used in the mapping driver for collection for example). So the stored value looks like
array("test1", "test2", ....)
Does anyone know which Doctrine-ODM mapping type I should use in order to preserve the key-value pairs in the database?
Thank you in advance,
Andi (greetz from germany)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它应该是哈希类型:
http://readthedocs.org/docs/doctrine-mongodb-odm/en/latest/reference/annotations-reference.html?highlight=hash#hash
It should be the Hash type:
http://readthedocs.org/docs/doctrine-mongodb-odm/en/latest/reference/annotations-reference.html?highlight=hash#hash
对于ODM 2.0之前的版本,@Hash将提供必要的数据类型。然而,在 ODM 2.0 之后,@Hash 字段将被删除。为了使用它,我们必须使用散列类型的@field。
如需进一步参考
[点击此处][1]
For versions before ODM 2.0 @Hash will provide the necessary data type. However after ODM 2.0 @Hash field is being removed. In order to use it we have to use @field with type hash.
For further reference
[click here][1]
我认为您正在寻找
hash
数据类型。你不是吗?I think you're looking for
hash
data type. Aren't you?最好的答案是使用 哈希类型。但如果由于某种原因你不想使用
hash
类型,你可以使用 Doctrine ODM 提供的 EmbeddedDocument 功能,如文档所述:因此,您需要在
AppBundle\Document\EmbeddedExample.php
中创建 EmbeddedDocumentEmbeddedExample
:然后,您可以在
Test 中使用
文档。因此EmbeddedExample
Test.php
文件将与此类似: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:So, you need to create EmbeddedDocument
EmbeddedExample
inAppBundle\Document\EmbeddedExample.php
:Then, you can use
EmbeddedExample
in yourTest
document. So theTest.php
file will be similar to this:@Array 应该可以工作。 ORM 中至少存在等效项 (@Column(type="array"))
@Array should work. At least an equivalent exists in the ORM (@Column(type="array"))