Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Embedded data model?

Hi there! How to embed a document to another document?

Here is the example from the mongodb official documentation:

{
   _id: "joe",
   name: "Joe Bookreader",
   address: {
              street: "123 Fake Street",
              city: "Faketon",
              state: "MA",
              zip: "12345"
            }
}

For this user I need to create a model like this:

class User extends \Phalcon\Mvc\Collection {

    public $id;
    public $name;

}

Ok, and what about an object with an adress? Is it an additional object extended from \Phalcon\Mvc\Collection? What if it an array of objects like this:

{
   _id: "joe",
   name: "Joe Bookreader",
   addresses: [
                {
                  street: "123 Fake Street",
                  city: "Faketon",
                  state: "MA",
                  zip: "12345"
                },
                {
                  street: "1 Some Other Street",
                  city: "Boston",
                  state: "MA",
                  zip: "12345"
                }
              ]
 }

So, give me an example, please.



33.6k
Accepted
answer

It's just an attribute of the collection:

class User extends \Phalcon\Mvc\Collection {

    public $id;
    public $name;
    public $address;
}