We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



34.6k
Accepted
answer

It's just an attribute of the collection:

class User extends \Phalcon\Mvc\Collection {

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