Magento2 EAV Attribute – How to Load Value from Database

attributeseavmagento2

During some tasks, I was not aware of exactly how Magento loads values for attributes that are available. for example, if we have loaded Product Model and attributes in $product->getData(). Which classes and methods are responsible for loading text attribute values?

I'm searching how Magento loads attribute values for \Magento\CatalogImportExport\Model\Export\Product but cant figure it out. I ran debug over and over again. but, it's a massive object-oriented relations code. Can anyone point to the place that is responsible for that? Xdebug points me to \Magento\Eav\Model\ResourceModel\Entity\Attribute but the magic happens there and we have value :).

But, I want to know how it's working now?

PS. happy for me that it was not needed for the task, but now I need to figure out how it work for myself.

Best Answer

You can load product in Magento with 2 ways, load single product and load collection. As I correct understand you are interest how Magento EAV works for single load. In this case:

The following methods are generally doing the same

  • \Magento\Catalog\Api\ProductRepositoryInterface::get()
  • \Magento\Catalog\Api\ProductRepositoryInterface::getById()
  • \Magento\Catalog\Model\Product::load()

How it works

  1. Product Model pass load to it resource model \Magento\Catalog\Model\ResourceModel\Product.
  2. Resource model load base entity row and call loadAttributesForObjectmethod to determine which attribute should be loaded. By default uses \Magento\Eav\Model\Entity\AttributeLoader::loadAllAttributes logic.
  3. Resource Model delegate load attributes to Entity Manager (\Magento\Framework\EntityManager\EntityManager::load) and pass product model.
  4. Entity Manager detect read (\Magento\Framework\EntityManager\Operation\Read) operation from \Magento\Framework\EntityManager\OperationPool
  5. Based on entity metadata Read Operation calls read attributes (\Magento\Framework\EntityManager\Operation\Read\ReadAttributes::execute) and read extensions (\Magento\Framework\EntityManager\Operation\Read\ReadExtensions::execute) in execute method
  6. Based on Attribute Pool (\Magento\Framework\EntityManager\Operation\AttributePool) the system collect read actions for entity (see di.xml)
  7. The system runs execute for each actions and loads default attributes data in \Magento\Eav\Model\ResourceModel\ReadHandler::execute and collect product data
  8. Hydrator (\Magento\Catalog\Model\Product\Hydrator) sets collected data into product model
Related Topic