Creating an EAV Entity

Posted Almost 10 years ago. Visible to the public.

Overview

A overview for those familiar with making flat models:

  • Your config is standard, your table name takes the form of the base (entity) table
  • You name your resource models as normal (so by class name, you can't tell the difference between flat and EAV)
  • You make your domain model as normal
  • Your resource model extends a different class and has different _construct() code
  • Your resource collection model extends a different class, but is otherwise the same
  • You make your entity and value tables using setup DDL as you would with any table
  • Your entity types, attributes, sets and groups are set up for you by using getDefaultEntities()

A Bit More In-depth

Slightly more in depth, but pretty rough:

  1. Set up your config as normal

  2. Make your domain model as normal

  3. Create your resource model as normal, but with two exceptions:

    • Extend Mage_Eav_Model_Entity_Abstract

    • _construct() should look like this:

        /**
         * Resource initialization
         */
        protected function _construct()
        {
          $resource = Mage::getSingleton('core/resource');
          $this->setType('entity_type_code')->setConnection(
              $resource->getConnection('yourmodule_read'),
              $resource->getConnection('yourmodule_write')
          );
        }
      
  4. Make a collection as normal, but with one exception:

    • Extend Mage_Eav_Model_Entity_Collection_Abstract
  5. Make your setup resource, remember to extend Mage_Eav_Model_Entity_Setup for EAV goodness

  6. In your setup class, make a getDefaultEntities() method to install your entities. See the file examples at the bottom for what this looks like

  7. Make a setup script which calls $installer->installEntities();. Note that this only sets up the entity types, attributes and sets - it doesn't make the actual tables, the below stuff does that

  8. Add table creation to your setup script. You might think there is a lovely magic way of doing this, well you'd be right, but no-one uses it because it's overbearing in the sense it creates ALL the possible value tables you could ever want (you might not need them all) and it also adds store_id and increment_id to the entity table, which are most likely pointless for your needs, so instead Magento simply uses normal DDL creation statements to make all the tables - yep, that's a lot of code! See the file examples at the bottom for what this looks like

The Files

Can't be bothered to read? Just use these Show archive.org snapshot . Gists don't allow subdirectories, so I've used underscores.

Other Links

Mike Whitby
Posted by Mike Whitby to Magento (2014-05-23 09:32)