In this tutorial, we will explore our journey through Magento’s EAV Model further by showing how we can easily load and save an EAV entity. First, let’s discover the EAV entity’s structure and then I’ll show you the way to load and save entities in details.

1. Structure of an EAV entity

  • An entity contains data taken from different tables. When you call a load command, tables will join together to provide data for this entity.
  • Objects are entities, and object properties are attributes.
  • The differences between the entity eav and the resource model

Entities extend Magento’s resource objects and resources simply connect with the database (actually they manage the different reading/writing connections and automatically figure out table names based on the convention). Basically, Entities are core things that pair up to selected Models and help them save to the database.

2. Load and save an entity

• Load: Load entity’s attributes into the object

  • Read data from the main table (main table or entity table)
  • Define the attribute set of the object
  • Read the attribute’s value of the object
  • Change the attribute’s value (through the backend_model of the attribute)
  • Map date into the object
public function load($object, $entityId, $attributes=array())
{
Varien_Profiler::start('__EAV_LOAD_MODEL__');
/**
* Load object base row data
*/
$select = $this->_getLoadRowSelect($object, $entityId);
$row = $this->_getReadAdapter()->fetchRow($select);
//$object->setData($row);
if (is_array($row)) {
$object->addData($row);
}

if (empty($attributes)) {
$this->loadAllAttributes($object);
} else {
foreach ($attributes as $attrCode) {
$this->getAttribute($attrCode);
}
}

/**
* Load data for entity attributes
*/
Varien_Profiler::start('__EAV_LOAD_MODEL_ATTRIBUTES__');
$selects = array();
foreach ($this->getAttributesByTable() as $table=>$attributes) {
$selects[] = $this->_getLoadAttributesSelect($object, $table);
}
if (!empty($selects)) {
$select = $this->_prepareLoadSelect($selects);
$values = $this->_getReadAdapter()->fetchAll($select);
foreach ($values as $valueRow) {
$this->_setAttribteValue($object, $valueRow);
}
}

Varien_Profiler::stop('__EAV_LOAD_MODEL_ATTRIBUTES__');

$object->setOrigData();
Varien_Profiler::start('__EAV_LOAD_MODEL_AFTER_LOAD__');
$this->_afterLoad($object);
Varien_Profiler::stop('__EAV_LOAD_MODEL_AFTER_LOAD__');

Varien_Profiler::stop('__EAV_LOAD_MODEL__');
return $this;
}

• Save: Save entity’s attributes into the object’s resource

  • Take data mapped in the object
  • Change the attribute’s value (through the backend_model of the attribute)
  • Save data to the main table (main table hay entity table)
  • Save data to the attribute’s value table

• The differences in loading and saving between eav entities and normal models

Do you find the information above interesting? Nice code! 😉

How to pass Magento certification exam in 30 days

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

13 Comments

  1. Thanks a lot for this article. it help me to prepare the certification.

    Very good description, easy to understand and very clear.

    Again, thanks for your works.

    Pierre

    • Thanks Pierre. Hope that you will continue following our blog and the next posts will help you more.

  2. Thanks for your encouragement. I’ve visited your page and read your tutorials. They are also really interesting. Keep writing, Pierre. I’ll be very delighted if we can discuss and share the useful knowledge of Magento 😉

  3. yeah sure it will be cool. Add me on twitter and i give you my Skype and my googletalk account if you want.

    It’s always good to have magento friends to discuss and to helps us when having questions.

    https://twitter.com/#!/pierrefay

    See you soon 😉

  4. Rajesh Kumar Jain Reply

    Its really useful post for me.

    Hey can you please clear my doubt please?

    Why eav_entity table is always empty after adding products attributes attributes sets, categories and many products but still this table is empty.

    Thanks in advance 🙂

    • Hi Rajesh Kumar Jain,
      Table eav_entity is empty because the category and product entity is not stored on that table.
      Magento allows each entity type can stored on it’s eav table (product is stored in catalog_product_entity, category is stored in catalog_category_entity).
      You can see the method getDefaultEntities() on class Mage_Catalog_Model_Resource_Eav_Mysql4_Setup to view more details, it setup product and category entity with table is not eav_entity.

  5. Hi David,

    It means that magento does not use eav_entity table and if we delete this table, it does not affect magento database. Is that correct?

    • Hi Rajesh,

      Yes. But if you have a custom module which installs database with EAV model, you may be need that table.

  6. Thank you, very much David. 🙂 … Now I started reading your all posts from the start as I want to go for magento certification. I know, all the posts will be the best tutorial for me to understand clearly.
    May I add you in facebook?

    Rajesh (Magento Boy) 🙂

  7. hey can any one tell me what is the life cycle of database connection in magento ?
    please explain me in details

Write A Comment