38. Magento-Meetup Wien
Ivan Cuk
Software Developer
From Core to Custom
A Deep Dive into Magento Entity Extension
What is a Magento Entity?
● Entity is data item (customer, product, order & more)
● Each item has its attributes and values for them
● Represented with one main entity database table (and few other tables)
Why this topic?
● Extending core entities is frequently needed for
customizations
● Important for the stability, modularity, upgradability
and overall quality of the Magento Store
● Different approaches (even within same teams)
Magento core entities
EAV Not EAV
customer order
customer_address invoice
catalog_category creditmemo
catalog_product shipment
EAV available with some entities, Extension Attributes with all entities
The Problem
When to use EAV and when to use Extension Attributes?
How to properly implement Extension Attributes and why
is that important?
Often seen approaches
Developers tend to some of the following things:
● Not use Extension Attributes at all: use a (hacky)
workaround
● Implement Extension Attributes almost always: EAV
capabilities are getting heavily overlooked
EAV Attributes
Basic EAV Attributes can be developed quickly.
No-brainer situation:
Attributes that have simple scalar values.
Example: a custom ERP-ID attribute for the customer
entity
EAV Attributes
Often overlooked: EAV Attributes can be also used for
more complex data-processing and functionalities.
EAV-Models:
attribute_model - overall behaviour
backend_model - before and after attribute save
frontend_model - data representation
source_model - retrieving set of choices
Extension Attributes
When?
● Entity without EAV
● The complexity of the new custom attributes requires
even more flexibility than what EAV offers
Extension Attributes
Advantages
● Unlimited flexibility
● Data saved in its own new tables
Extension Attributes
Disadvantage
● Implementation is can be complex and time consuming
● Some developers turn to hacking the core-tables as an
alternative
Modifying Core Tables?
● Not recommended - source:
https://experienceleague.adobe.com/docs/commerce-
operations/implementation-playbook/best-
practices/development/modifying-core-and-third-party-tables.html?lang=en
● Decreases upgradability & stability of the project
● Underlying logic contains raw SQL queries - can cause side effects difficult to
troubleshoot
● Changes can cause performance problems
● 3rd party modules expect standard core tables
● Standard ways are safer:
● With EAV-Attributes we are not changing DB-structure
● Extension Attributes have their own tables
Modifying Core Tables?
● We are using or relying on directly the implementation (model) classes
instead of Service Contracts
● Example: $model->getData('erp_id');
Service Contracts > Implementations
Extension Attributes
Not always used feature of extension attributes that can accelerate development:
● Join directives
Extension Attributes
Not always used feature of extension attributes that can accelerate development:
● Join directives
Conclusion
● Requirements & custom attributes
● The right implementation
● Full potential of the EAV attributes
● All features of the Extension Attributes
● Avoid hacky solutions when possible

38. Magento Meetup Austria: Ivan Cuk - From Core to Custom. A Deep Dive into Magento Entity Extension

  • 1.
    38. Magento-Meetup Wien IvanCuk Software Developer From Core to Custom A Deep Dive into Magento Entity Extension
  • 2.
    What is aMagento Entity? ● Entity is data item (customer, product, order & more) ● Each item has its attributes and values for them ● Represented with one main entity database table (and few other tables)
  • 3.
    Why this topic? ●Extending core entities is frequently needed for customizations ● Important for the stability, modularity, upgradability and overall quality of the Magento Store ● Different approaches (even within same teams)
  • 4.
    Magento core entities EAVNot EAV customer order customer_address invoice catalog_category creditmemo catalog_product shipment EAV available with some entities, Extension Attributes with all entities
  • 5.
    The Problem When touse EAV and when to use Extension Attributes? How to properly implement Extension Attributes and why is that important?
  • 6.
    Often seen approaches Developerstend to some of the following things: ● Not use Extension Attributes at all: use a (hacky) workaround ● Implement Extension Attributes almost always: EAV capabilities are getting heavily overlooked
  • 7.
    EAV Attributes Basic EAVAttributes can be developed quickly. No-brainer situation: Attributes that have simple scalar values. Example: a custom ERP-ID attribute for the customer entity
  • 8.
    EAV Attributes Often overlooked:EAV Attributes can be also used for more complex data-processing and functionalities. EAV-Models: attribute_model - overall behaviour backend_model - before and after attribute save frontend_model - data representation source_model - retrieving set of choices
  • 9.
    Extension Attributes When? ● Entitywithout EAV ● The complexity of the new custom attributes requires even more flexibility than what EAV offers
  • 10.
    Extension Attributes Advantages ● Unlimitedflexibility ● Data saved in its own new tables
  • 11.
    Extension Attributes Disadvantage ● Implementationis can be complex and time consuming ● Some developers turn to hacking the core-tables as an alternative
  • 12.
    Modifying Core Tables? ●Not recommended - source: https://experienceleague.adobe.com/docs/commerce- operations/implementation-playbook/best- practices/development/modifying-core-and-third-party-tables.html?lang=en ● Decreases upgradability & stability of the project ● Underlying logic contains raw SQL queries - can cause side effects difficult to troubleshoot ● Changes can cause performance problems ● 3rd party modules expect standard core tables ● Standard ways are safer: ● With EAV-Attributes we are not changing DB-structure ● Extension Attributes have their own tables
  • 13.
    Modifying Core Tables? ●We are using or relying on directly the implementation (model) classes instead of Service Contracts ● Example: $model->getData('erp_id');
  • 14.
    Service Contracts >Implementations
  • 15.
    Extension Attributes Not alwaysused feature of extension attributes that can accelerate development: ● Join directives
  • 16.
    Extension Attributes Not alwaysused feature of extension attributes that can accelerate development: ● Join directives
  • 17.
    Conclusion ● Requirements &custom attributes ● The right implementation ● Full potential of the EAV attributes ● All features of the Extension Attributes ● Avoid hacky solutions when possible