Adding a new attribute

Last modified by amartinez on Fri, June 25, 2010 09:15
Source|Old Revisions  

In order to add a new attribute to your magento installation, you can use following method:

Create an instance of Mage_Eav_Model_Entity_Setup class and use its method addAttribute() to add one.

  1. $code = 'my_attribute';
  3. $attr = array(
  4.   'entity_type_id' => $entity_type_id,
  5.   'backend_type' => 'int',
  6.   'is_user_defined' => 1,
  7.   'frontend_input' => 'text',
  8.   'is_visible' => 0,
  9. );
  11. $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
  12. $setup->addAttribute($entityTypeId, $code, $attr);

$attr is an array containing Attribute elements (like frontend and backend model, default value, etc.) Look at eav_attribute table for all possible keys. $entity_type_id is a value from the db table eav_entity_type.entity_type_id

For details look at the sourcecode:



You can get entity_type with $entityTypeId = Mage::getModel(’eav/entity’)→setType(’catalog_product’)→getTypeId();