Magento’s EAV database model allows for easy extension of its current attributes associated with different entities. Credit for this needs to go to Alan Storm as he was the one to show me this when I needed it. Note – this method is valid for version 1.5.x.x, I’m not certain this will work on anything prior.

Extending an entity’s attributes is done by executing the addAttribute method on the Mage_Eav_Model_Entity_Setup class. I’ve seen references to add attributes using this class by instantiating the class using something like:

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

But it really only makes sense to run this code once – preferably during the installation of the module. So if your module doesn’t have one already, add a setup script by adding the following to your module’s config.xml. For demonstration purposes, we’ll assume our package name is Package and our app is called Myapp.

<?xml version="1.0"?>

Now create a setup file in your modules directory in sql/myapp_setup/mysql4-install-0.0.1.php, remembering to change the version number to correspond to the version number in your config.xml. Here, we can get an instance of the Mage_Eav_Model_Entity_Setup simply by referencing $this. Here’s an example of adding an attribute to the Custom model.

addAttribute('customer', 'custom_field', array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'Custom Field',
    'visible' => false,
    'required' => false,
    'position' => 69,

As you can see this method takes three parameters: an entity type, a unique identifier for this attribute, and an array of the attribute’s options. There are many options available here, you can pretty much set a value for any column on the eav_attribute table, although there’s a _prepareValues method that sets defaults and reads shorter versions of the column names. This method is located in: app/code/core/Mage/Eav/Model/Entity/Setup.php

 * Prepare attribute values to save
 * @param array $attr
 * @return array
protected function _prepareValues($attr)
    $data = array();
    $data = array(
        'backend_model'             => $this->_getValue($attr, 'backend', ''),
        'backend_type'              => $this->_getValue($attr, 'type', 'varchar'),
        'backend_table'             => $this->_getValue($attr, 'table', ''),
        'frontend_model'            => $this->_getValue($attr, 'frontend', ''),
        'frontend_input'            => $this->_getValue($attr, 'input', 'text'),
        'frontend_label'            => $this->_getValue($attr, 'label', ''),
        'frontend_class'            => $this->_getValue($attr, 'frontend_class', ''),
        'source_model'              => $this->_getValue($attr, 'source', ''),
        'is_required'               => $this->_getValue($attr, 'required', 1),
        'is_user_defined'           => $this->_getValue($attr, 'user_defined', 0),
        'default_value'             => $this->_getValue($attr, 'default', ''),
        'is_unique'                 => $this->_getValue($attr, 'unique', 0),
        'note'                      => $this->_getValue($attr, 'note', ''),
    return $data;

So on the left, the key of the $data array is the database column, and the _getValue method is reading the name you use in your attribute options when calling the addAttribute method. The third parameter of the _getValue is the default value should you omit the value from your options array.

Beyond these values, you can also specify a ‘group’ to assign your new attribute to a group set, and ‘option’ to pass in an array of values to be used in a select list. I haven’t tested these out extensively, let me know if you discover how these are used.

Also, remember that if your setup script has already run for your module and you need to run it again simply remove the row where code = 'myapp_setup' in the core_resource table. Your setup script should then run the next time a page is rendered.

Share this article:

If this resonates with you I'd love to help!

I help business with problems like these every day.

Click below to schedule a call.

About The Author

Bryce Hamrick

Facebook Twitter

Bryce Hamrick is an entrepreneur, business & marketing strategist, and product consultant with nearly two decades of experience in industry. Bryce has been a software engineer, product manager, and director of product management for startups as well as large enterprises. He has led teams to bring dozens of products to market and has executed numerous six-figure product launches. Today Bryce and his team focus on leveraging his product execution strategy to help businesses with growth and scale.