Extending entities

We extend entities by adding additional attributes to them. Referring back to the magical getter and setter methods mentioned in the context of MagentoFrameworkDataObject, the logical thinking might be: what's the big deal; can't we just add new database columns via UpgradeSchema and use magical getter and setter methods to go around it? The answer is both yes and no, but mainly leaning toward no – we will soon learn why.

To better explain this, let's take a look at MagentoSalesModelOrder, the entity model. This model implements the MagentoSalesApiDataOrderInterface interface, which further extends MagentoFrameworkApiExtensibleDataInterface. Here, we can see a constant defining a key for the extension attributes object. This is somewhat of a starting point for extending entities. Suffice to say, there is an extra abstraction layer on top of some of the models. This abstraction layer, called service contracts, is a set of PHP interfaces that ensure a well-defined, durable API that other modules and third-party extensions might implement.

This, however, is easier said than done. When you think about it, if we had a module that's already heavily in use, adding even a simple attribute to one of its entity models might break its functionality. This is where extension attributes come into the picture.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.22.135