1
votes

My problem is the following: I have a mongo database which stores such objects { "name":"Accord Neo", "number_of_photos":"3", "id":"accord_neo", "description":"Very comfortable sofa.", "details": { "chair_is": false, "Sofa_bed": "delfin", "Structure_configuration": "corner" }, "properties":[ { "property":"2-У-1", "value":"2150 X 1550 X 880" }, { "property":"Sleeping place", "value":"2150 X 1550 X 880" } ] }​

I need to retrieve these objects in Symfony2 using Doctrine Mapping Types and the problem is that I don't understand how to create my Custom Classes for Mapping for the field "details" and the field "properties". I tried to make analogy from the official website of doctrine http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/basic-mapping.html#custom-mapping-types but I still don't undertand how it works and how it should be implemented in my case.

1

1 Answers

0
votes

Create a new Document for "details" like this:

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\EmbeddedDocument
 */
class Details
{
}

Note that it's an embeddeddocument so it will be stored as part of the one record in mongodb. Do exactly the same thing for Properties

Then in your parent object, you embed a set of them and create the collection in your constructor:

/**
 * @MongoDB\EmbedOne(targetDocument="Details")
 */
protected $details;

/**
 * @MongoDB\EmbedMany(targetDocument="Properties")
 */
protected $propertieslist;

public function __construct()
{
        $this->propertieslist = new ArrayCollection();
}

You can generate your setters and getters using the tools that come with symfony.

Assuming you're using Forms to manage them, you need to create a FormType for each embedded document.

For the details, you just go "new DetailsType" as your type (as opposed to "text" or "choice").

For the properties, you'll need to add a 'collection' instead, and then pass an array where 'type' => new PropertiesType. To add and remove dynamically is going to need a bit of javascript. Details are here: http://symfony.com/doc/current/cookbook/form/form_collections.html