Search code examples
mongodbsymfonydoctrine-odm

How to embed MongoDB documents in Symfony 3.4


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.


Solution

  • 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