M
M
Michael2018-04-12 18:05:00
Magento
Michael, 2018-04-12 18:05:00

How to work with extension attributes in m2?

Every project sooner or later requires adding custom attributes. For example, it was necessary to store an additional date in the order. The first thing to do is open the documentation . It says that it is possible to extend models using custom and extension attributes. As a rule, custom are used more as attributes for the user, and extension - as system ones. We create the extension_attributes.xml file and add the necessary attributes to the entities in it. In our case, the date to the order model.
So, according to the documentation, we have done everything and now we can start using the attributes ... but they do not work. Oh yes, you need to clear the cache. Then we clear and be sure to remove the generated code from /generated. It seems like they didn't forget anything. Well, now the system does not swear at non-existent attributes, but again we notice that again nothing works.
After a couple of days (for someone else), it becomes clear that in order to ensure the work of our expensive attributes, it turns out that we need to do a special implementation. And again, the most interesting is ahead. Having made an implementation for the Repository, for reading and writing separately - what do you think? - suddenly it turns out that the collections have attributes... right, === null. No, well, who loves to immediately and conveniently. Obviously not magenta developers. There is nothing left but to make an implementation for collection. And don't forget the afterGetExtensionAttributes plugin. It will do a check: if the attributes === null, then we create them. Don't ask why.
After many efforts... no, no, and again no: nothing will work if you try to load the entity via load in the model. To say that the system is overcomplicated is to say nothing. Do you know any advice on this on SO? Offer to load the [order] entity again. Great, after so many optimizations, developers have to load entities at every corner for the sake of attributes.
When working with attributes, I would like to add my own methods to the entity, but this cannot be done. No, methods can be added, but attributes may not be loaded, which kills the whole possibility. Someone creates their own models inherited from the entity and uses them separately, being sure that all data is loaded.
Probably, I do not know something and made mistakes somewhere. I just want to understand how to start using extension attributes without pain, how to work with them correctly. Tell me, please, who understood how.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CodeKeeper, 2018-04-13
@CodeKeeper

First you need to understand what EAV entities are and how they work.
https://alanstorm.com/magento_advanced_orm_entity_...
Extesndion Attribute is nothing more than an eav attribute for an entity and you can work with it just like with any other Eav attribute.
What else is a separate implementation?
Correctly, this is just what was done for optimization, because when working with a large collection, you do not need to load all the information. To do this, there is addAttributeToSelect which adds attribute information to the resulting selection.
Because they don't know how EAV works and addAttributeToSelect

$collection->addAttributeToSelect('unit_of_sale', 'catalog_product_entity_varchar')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question