2

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

3

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Alright, will test that tonight and let you know. thank you very much ;)
Mhhh I am facing another issue...I am trying to guery that embedded document to display information on the twig view, but can only get the parent document, any idea how I get the Address information please? I tried that : Public function GuestAction() { $customer= $this->get('doctrine.odm.mongodb.document_manager') ->getRepository('TestBundle:cutomer') ->findAll(); return $this->render('TestBundle:Default:test.html.twig', array('customer' => array('purshaedItems' => 'purshaedItems' ))) ; }
Anyone please? I'm really stuck for more than 24 hours trying to get it workings :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.