Magento EAV Setup Scripts

In the blog post we will look into setup class for EAV attribute and the different available in it.

The base class for setup is Mage_Eav_Model_Entity_Setup
Lets look at some important function which this class offers how its useful.

Below the functions we look at, which work on object of class ‘eav/entity_setup’. To make an instance of this class outside setup script simply write

$class = new Mage_Eav_Model_Entity_Setup();

and inside setup script, we can use $this

Attribute Set Related Functions

$class->addAttributeSet('customer', 'Test Attribute Set');

Here ‘customer’ is the entity type which we have taken from ‘eav_entity_type’ table.
The above code make an entry in table ‘eav_attribute_set’
Similar we have functions like

$class->updateAttributeSet('customer','Default','attribute_set_name','New Name');
$class->getAttributeSetSortOrder('customer'); //returns the next available attribute set sort order

Attribute Group Related Functions

$class->addAttributeGroup('customer','Default','Test Group Name');

This will make updates to table ‘eav_attribute_group’
Other functions we have related to attribute group are

$class->updateAttributeGroup('customer', 'Default', 'General', 'attribute_group_name', 'New Name');
$class->updateAttributeGroup('customer', 'Default', 'General', 'is_default', 0);

Attribute Related Functions

To add new attribute

       $class->addAttribute('customer', 'test_code', array(
            'backend_model' => null,
            'backend_type' => 'varchar',
            'backend_table' => null,
            'frontend_model' => null,
            'frontend_input' => null,
            'frontend_label' => 'Test Attribute',
            'frontend_class' => null,
            'source_model' => null,
            'is_required' => 0,
            'is_user_defined' => 1,
            'default_value' => '',
            'is_unique' => 0,
            'note' => '',
            'is_global' => 0,
            'group' => 'General',
            'option' => array(
                'values' => array(

This will add entry in table ‘eav_attribute’ where it will add new attribute. Also entry in table ‘eav_attribute_group’ and ‘eav_attribute_set’ where it will create a new attribute group and assigned it default attribute set. Next it will create options in ‘eav_attribute_option’ and ‘eav_attribute_option_value’ table.

We have other functions like