1

I´m new in Symfony 3.4 and I am try to embed a document into another document, like this:

{ name:"alex", age: 18, schoolGrades:{ elementary: "yes", highScholl: "yes", college: "no" } }

I´m using Doctrine ODM to work this, but I don´t know how I can do this.

Robert_Rmz
  • 47
  • 1
  • 8

1 Answers1

1

There's a concept of Embedded documents that does exactly that:

/** @Document */
class Student
{
  /** @EmbedOne(targetDocument="SchoolGrades") */
  private $schoolGrades;
}

/** @EmbeddedDocument */
class SchoolGrades
{
}

https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/1.2/reference/embedded-mapping.html

  • Thanks for your answer @Небојша Камбер. I understand that to save the data of the document these should be mapped and then generate their setters and getters, the other question is if the embedded document will be filled from a form, I must map this document from the class where I declare it or I must put their setters an getters there? – Robert_Rmz Nov 23 '18 at 15:11
  • Embedded forms correspond to embedded documents. I usually make form type classes for each document class (`StudentType` and `SchoolGradesType`) and then add the child type in the parent type (`->add('schoolGrades', SchoolGradesType::class)` in the `StudentType`) – Небојша Камбер Nov 27 '18 at 11:01
  • Thanks again @Небојша Камбер, I was reviewing the embedded forms, but I didn´t understand what they were used for, you would have a more complete example of embedded classes and how you could implement them in an embedded form that you could show me? – Robert_Rmz Dec 06 '18 at 19:04
  • It's very simple, for example https://gist.github.com/nebkam/6c7488685e0bcb10e38960cfff27e23a According to documentation https://symfony.com/doc/3.4/forms.html – Небојша Камбер Dec 07 '18 at 10:38