[英]Associative array of EmbedMany documents in Doctrine ODM
I am trying to create an associative array of embedMany relationship with string keys for example I have a Document class named sampleDocument
and it has an EmbedMany relationship on the field price
. 我正在尝试使用字符串键创建embedMany关系的关联数组,例如,我有一个名为sampleDocument
的Document类,并且在字段price
上具有EmbedMany关系。
this is how I am setting the price field with associative array with string keys: 这就是我使用带字符串键的关联数组设置价格字段的方式:
$obj->setPrice(array('key1' => $priceObjOne, 'key2' => $priceObj2));
it insert that data into the mongo as follows: 它将数据插入到mongo中,如下所示:
{
"prices": [
{/*Price Object*/}, { /*Price Object*/ }
]
}
What I am trying to do is to have a string key for each one. 我正在尝试做的是每个都有一个字符串键。 it already has the string key in the array. 它已经在数组中具有字符串键。 but it doesn't insert it into the Mongo. 但它不会将其插入Mongo。 how can I get passed it? 我该如何通过?
You need to define "prices" as @Hash type in your object. 您需要在对象中将“价格”定义为@Hash类型。 With “type” attribute set to “hash”, Doctrine will store and retrieve the value as an associative array: 将“类型”属性设置为“哈希”后,Doctrine将存储和检索该值作为关联数组:
<?php
class SampleDocument
{
/** @Hash */
protected $prices = array();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.