Mapping - Mongodb Embedded Document in Symfony2

天涯浪子 提交于 2019-12-24 00:24:26

问题


I am trying to add a mapping information in Symfony2 using MongoDB as shown here : http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html [at the Adding Information section] But what I want to do is to add an embedded documents as well. Here is a part of my document with embedded documents :

"_id" : "",
"last_name" :,
"first_name" : "",
"address" : [
{
    "Street" : "",
    "City" : "",
    "Zip_Code" : "",
    "Country": ""

}
],
"company" : ""
"purshaed_items" : [
{
    "items_id" : "",
    "category":"",
    "price":"",
    "date_of_purshae"

}
]

So as you can see I have the fields "Address" & "Purshaed_items" that are embedded documents. I found this link http://doctrine-orm.readthedocs.org/projects/doctrine-mongodb-odm/en/latest/reference/embedded-mapping.html I guess that my case will be the "Embed Many" but didn't really get how it works?

Can someone please try to explain how it works?

Thank you


回答1:


I would make it like this:

/** @EmbedMany(targetDocument="PurshaedItem") */
    private $purshaedItems = array();

Assuming that you have another document class named PurshaedItem (PurchasedItem perhaps).

And the target document would be mapped like this:

/**
 * @MongoDB\EmbeddedDocument
 */
class PurchasedItem
{
  // whatever properties ...
}

And if you want to query for embedded documents, you can get it in controller (recommended), here is my simple example.

For getting it in twig template, here is another example.

The examples are from simple Symfony blog application. There are 3 documents: Post, Tag, and Comment. Post has Tags and Comments as embedded properties. Take a look around the code, it is simple. If the answer has been useful to you, please click to accept it.



来源:https://stackoverflow.com/questions/33822297/mapping-mongodb-embedded-document-in-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!