Home » Magento9 December 2009

Magento: Adding attribute from MySql setup file

You can add attribute from Admin Panel -> Catalog -> Attributes -> Manage Attributes.

You can also add attributes from mysql setup file of your module. MySql setup file is present inside “YourModule/sql/yourmodule_setup” directory.

In the following example, the version of my module is 0.1.0.  I have added attribute for product. I have added a new Attribute Group called “Special Attributes” and then I have assigned the new attribute called “testing_attribute” into the “Special Attribute” group.

Be careful on input and type. Input means the input type of the attribute. And type means the input type in database.

For textfield it will be:
'input' => 'text',
'type' => 'text',

For textarea it will be:
'input' => 'textarea',
'type' => 'text',

For date field it will be:
'input' => 'date',
'type' => 'datetime',

For select list it will be:
'input' => 'select',
'type' => 'text',

Here is the sample code to add datetime attribute. Please note that the type for date should be datetime and backend should be eav/entity_attribute_backend_datetime.

Adding custom options to dropdown attribute through setup file

You can also add custom options to dropdown attribute, like there is in manufacturer or color attribute.

Here is a sample code for this purpose:-

In the code above, we have added a dropdown attribute named ‘Test‘. Three options (Sony, Samsung, and Apple) are added to this attribute.

Note:- In the source value, you can see yourmodule/source_option. This means, you just need to create a file YourModule/Model/Source/Option.php with the following code:-

Following are the allowed parameters for attributes

(see Mage_Eav_Model_Entity_Setup::addAttribute)


From Mukesh Chapagain's Blog, post Magento: Adding attribute from MySql setup file

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • Chad

    This is great! Is there any way to add custom options of a drop down attribute through a setup file? Thanks!


    Thank you for this posting…….

  • http://blog.chapagain.com.np/ Mukesh

    Yes, we can add custom options to dropdown attribute through setup file. I have updated the article with your answer. Thanks.

  • Sushan Senanayake

    how can add customer type select box like this magento front end and backend?

  • Sergey Kolodyazhnyy

    > For select list it will be:
    > ‘input’ => ‘select’,
    > ‘type’ => ‘text’,
    You are wrong, type should be varchar or int. Text type you can use for texts like Description, Short Description etc.

  • PHPRelated

    Thank you. This was helpful.

  • Sandipgulati08

    how to add attribute in customer information in magento1.5.1

    plz tell

  • http://www.facebook.com/grigzie Andrew Griggs

    I’ve added an attribute into the sales_quote_item table using the mysql setup file (Thanks for showing how to do this!)

    I was wondering how you can edit this now? I can’t find an example of updateAttribute(). I want to change the mysql column so that it is a DATE column and not INT(10) which it is now.

    Thank again!

  • Caroline FRAMERY

    How do you do with a radio button ?

  • Amit Samtani

    HI Mukesh, Thanks for posting this it really has been very helpful.

    Quick question for you, I was trying to create an attribute that is filterable. But in Magento 1.5, the options that are in _prepareValues is as follows:

    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;   

    Its missing many attributes that you have in yours, like:

    ‘is_searchable’ => $this->_getValue($attr, ‘searchable’, 0),
    ‘is_filterable’ => $this->_getValue($attr, ‘filterable’, 0)

    Were those removed? Were they removed for a reason? Or did you override the Setup class?

    Looking forward to your response and again, thanks for posting this,

  • Pingback: Attribute in eigener Extension per Setup hinzufügen | Miragento()

  • Pingback: Magento tips « Peter Jaap's Blog()

  • Test

    How order option with multiselect??

  • Pingback: Adding new attributes | Magento Media()

  • Pingback: Magento Adding new customer attributes | Magento Media()

  • Cannycookie

    Do you know if its possible to allow a file upload attribute?

  • Peter

    you are a hero for posting stuff like this!!! Fact!

  • Sean Villani

    if your don’t want to make your own source table file, or are lazy:

    ‘source’ => ‘eav/entity_attribute_source_table’,

    Great post!

  • Soufiane Marar

    how about adding attribute to an existing group like General ?