In this post, we will try to understand what is EAV and how we can create our own EAV model.

To know more about type of models in Magento, click here

Creating EAV model requires a lot of steps, so follow each step carefully.

We need to create a module first. Click here to view the steps for creating a module.

Let us assume that we want EAV model for the entity Employee

  1. Create following directories in your module:
    Test\Basic\Model
    Test\Basic\Model\Resource
    Test\Basic\Model\Resource\Employee
    Test\Basic\sql
    Test\Basic\sql\basic_setup
  2. Now create following files in your module:
    Test\Basic\Model\Employee.php
    Test\Basic\Model\Resource\Employee.php
    Test\Basic\Model\Resource\Setup.php
    Test\Basic\Model\Resource\Employee\Collection.php
  3. Let us add codes to all the above mentioned files. First we will add <models> and <resources> node under the <global> node to define our entity and connection. Make sure your etc\config.xml file looks like this:
    <?xml version="1.0" ?>
    <config>
        <modules>
            <Test_Basic>
                <version>1.0</version>
            </Test_Basic>
        </modules>
        <global>
            <models>
                <basic>
                    <class>Test_Basic_Model</class>
                    <resourceModel>basic_resource</resourceModel>
                </basic>
                <basic_resource>
                    <class>Test_Basic_Model_Resource</class>
                    <entities>
                        <employee>
                            <table>basic_employee_entity</table>
                        </employee>
                        <employee_entity_varchar>
                            <table>basic_employee_entity_varchar</table>
                        </employee_entity_varchar>
                        <employee_entity_char>
                            <table>basic_employee_entity_char</table>
                        </employee_entity_char>
                        <employee_entity_datetime>
                            <table>basic_employee_entity_datetime</table>
                        </employee_entity_datetime>
                        <employee_entity_decimal>
                            <table>basic_employee_entity_decimal</table>
                        </employee_entity_decimal>
                        <employee_entity_text>
                            <table>basic_employee_entity_text</table>
                        </employee_entity_text>
                        <employee_entity_int>
                            <table>basic_employee_entity_int</table>
                        </employee_entity_int>
                    </entities>
                </basic_resource>
            </models>
            <resources>
                <basic_setup>
                    <setup>
                        <module>Test_Basic</module>
                        <class>Test_Basic_Model_Resource_Setup</class>
                    </setup>
                </basic_setup>
                <basic_write>
                    <connection>
                        <use>default_write</use>
                    </connection>
                </basic_write>
                <basic_read>
                    <connection>
                        <use>default_read</use>
                    </connection>
                </basic_read>
            </resources>
        </global>
    </config>
    
    

    Test\Basic\Model\Employee.php

    <?php
    class Test_Basic_Model_Employee extends Mage_Core_Model_Abstract
    {
        const ENTITY = 'basic_employee';
        protected $_eventPrefix = 'basic';
        protected $_eventObject = 'employee';
    
        function _construct()
        {
            $this->_init('basic/employee');
        }
    }

    Test\Basic\Model\Resource\Employee.php

    <?php
    class Test_Basic_Model_Resource_Employee extends Mage_Eav_Model_Entity_Abstract
    {
        function _construct()
        {
            $resource = Mage::getSingleton('core/resource');
    
            $this->setType(Test_Basic_Model_Employee::ENTITY);
    
            $this->setConnection(
                $resource->getConnection('basic_read'),
                $resource->getConnection('basic_write')
            );
        }
    }
    
    

    Test\Basic\Model\Resource\Employee\Collection.php

    <?php
    class Test_Basic_Model_Resource_Employee_Collection extends Mage_Eav_Model_Entity_Collection_Abstract
    {
        protected function _construct()
        {
            $this->_init('basic/employee');
        }
    }


    Here comes table specific settings.

    Test\Basic\Model\Resource\Setup.php

    <?php
    class Test_Basic_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup
    {
        public function getDefaultEntities()
        {
            return array(
                Test_Basic_Model_Employee::ENTITY => array(
                    'entity_model' => 'basic/employee',
                    'table' => 'basic/employee', /* Maps to the config.xml > global > models > basic_resource > entities > employee */
                    'attributes' => array(
                        'first_name' => array(
                            'type' => 'varchar',
                            'label' => 'First name',
                            'input' => 'text',
                            'required' => true,
                            'sort_order' => 10,
                            'position' => 10,
                            'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        ),
                        'last_name' => array(
                            'type' => 'varchar',
                            'label' => 'Last name',
                            'input' => 'text',
                            'required' => true,
                            'sort_order' => 20,
                            'position' => 20,
                            'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        ),
                        'email' => array(
                            'type' => 'varchar',
                            'label' => 'Email',
                            'input' => 'text',
                            'required' => true,
                            'sort_order' => 30,
                            'position' => 30,
                            'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        ),
                        'address' => array(
                            'type' => 'text',
                            'label' => 'Address',
                            'input' => 'multiline',
                            'sort_order' => 40,
                            'multiline_count' => 2,
                            'validate_rules' => 'a:2:{s:15:"max_text_length";i:255;s:15:"min_text_length";i:1;}',
                            'position' => 40,
                            'required' => false,
                            'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        ),
                        'is_active' => array(
                            'type' => 'int',
                            'label' => 'Is Active',
                            'input' => 'text',
                            'required' => false,
                            'sort_order' => 50,
                            'position' => 50,
                            'required' => false,
                            'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        ),
                    )
                )
            );
        }
    }

    Test\Basic\sql\basic_setup\install-0.1.0.php

    <?php
    $installer = $this;
    
    $installer->startSetup();
    
    /*
    * Create all entity tables
    */
    $installer->createEntityTables(
        $this->getTable('basic/employee')
    );
    
    /*
    * Add Entity type
    */
    $installer->addEntityType('basic_employee', Array(
        'entity_model' => 'basic/employee',
        'attribute_model' => '',
        'table' => 'basic/employee',
        'increment_model' => '',
        'increment_per_store' => '0'
    ));
    
    $installer->installEntities();
    
    $installer->endSetup();
  4. OK, we have done a lot of work now. Make sure to refresh your cache.
  5. To test our EAV model, let’s create the following test file: eav-test.php in the root directory of website and add following code in it-
    <?php
        require 'app/Mage.php';
        
        Mage::app();
        
        $employee = Mage::getModel('basic/employee');
        
        $employee->setFirstName('Ashutosh');
        $employee->setLastName('Pandey');
        $employee->setEmail('abc@xyz.com');
        $employee->setAddress('Noida, India');
        $employee->setIsActive(1);
        
        $employee->save();
        
        echo "Record created";

    Now check this file by visiting the url:

    http://localhost/magento/eav-test.php

    Go and check your database, you should see values in your tables.