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)
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]
I think you're looking for
hash
data type. Aren't you?@Array should work. At least an equivalent exists in the ORM (@Column(type="array"))
It should be the Hash type:
http://readthedocs.org/docs/doctrine-mongodb-odm/en/latest/reference/annotations-reference.html?highlight=hash#hash
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: