Page tree
Skip to end of metadata
Go to start of metadata

This is a quick tutorial on how to install custom product attributes (eav attributes) with your module. It can easily be adapted for other entities’ attributes. It’s also a good starting point for general install scripts.





When the Mage application loads it looks for custom install scripts in the modules sql/mymodule_setup/ folder and checks them against the current module version and the _core_resource table to see whether they should be executed.

Custom attributes (EAV attributes) are available for products, categories, customers and other modules. They are organised into groups (loosely correspond to left-hand tabs in admin’s Edit Product, etc pages) which in turn can be organised intosets (eg Manage Attribute Sets in the backend). They can have options (such as for dropdowns) which are specified in the database for manually created attributes or via class definitions for code created ones. Additionally, attribute values are stored in separate tables according to their datatype (int,varchar,text,decimal,datetime...)


Installing Custom Product Attributes



The following steps can be taken before OR after the module has been installed into Mage.


Step 1: Create the Resource Setup Class


Create the folder MyModule/Model/Resource/Eav/Mysql4/ in your module folder. Create a file called Setup.php and make a class definition as follows:

  1. class MyModule_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
  2. {
  3. }

...where MyModule_Model is what is defined as the base model in config.xml:

 Expand source

Note, mymodname defines the “short” version of the modules name used in Mage::getModel(’mymodname/mymodel’).

We’ll come back to this file in just a minute...

Step 2: Define the installer resource in config.xml

Add a <resource> block under <global> in the module’s etc/config.xml file:

 Expand source


Not certain whether <mymodname> and <mymodname_setup> need to correlate. Nor am I certain what the MyModule in<setup> needs to correspond to. The value in <class>, however, needs to be the same as the class you create in step 1.

Important: <mymodname_setup> is used to register the resource you’ll be using, in the database and specifically the core_resource table. That is where all module resources, core, local and community are registered. Unfortunately there is no differentiation between core, local and community which means that if your module has the same name as an existing module, regardless of the namespace, and both modules use <mymodname_setup>, they’ll clash. I’m not aware whether this is the whole extent of the repercussions, but this will stop mysql4-install-x.x.x.php from running. To avoid this you can use <mynamespace_mymodname> instead of <mymodname_setup>.

Step 3: Create an install script

Create the folders/file MyModule/sql/mymodname_setup/mysql4-install-1.2.3.php where mymodname_setupcorresponds to the XML entity you just added in config.xml.

Also ensure the 1.2.3 corresponds to the module version in config.xml

 Expand source


In the file add the following code:




 Expand source

$this is an instance of your MyModule_Model_Resource_Eav_Mysql4_Setup class created in the first step. Additionally, by extending Mage_Eav_Model_Entity_Setup you get access to the nifty installEntities() method which we’ll discuss below...

Step 4: Define your entities

Go back to your Setup.php file from the first step. By extending Mage_Eav_Model_Entity_Setup we get access to 2 important methods. The first is installEntities() which is called by our install script. The second is getDefaultEntities() which is called by installEntities() and defines our custom attributes. It returns a nested array defining entity types and their attributes.


From Magento 1.4, don’t forget to add these following lines. Between ‘table’ and ‘attributes’ elements. ;)

Without that, the General, Meta, Images, ... Tabs will not showing up in the product edit page.

  1. 'additional_attribute_table' => 'catalog/eav_attribute',
  2. 'entity_attribute_collection' => 'catalog/product_attribute_collection',


 Expand source


A good reference for all this is app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Setup.php

entity_model and table can be changed to add attributes to other entities (eg Customers). attributes is an array of arrays - one for each attribute you wish to define. To get an idea about the values possible, create your custom attributes manually via Product > Manage Attributes and then have a peak _eav_attribute table and replicate the values in this array.

A few of the less obvious ones:

classValidation class for data. Example values: validate-digitsvalidate-number
backendModel for handling input in the backend (eg customer/customer_attribute_backend_password)
frontendModel for handling display of attribute on the frontend (eg catalog/product_attribute_frontend_image)
sourceModel defining values for select boxes/dropdowns (see below)

Step 5: Run (or re-run) the install script

Now be sure to disable or clear the EAV cache and reload the edit product page in the backend (or any page I believe). The new attributes should be available to edit in the default set!

If you mess up or wish to re-execute the install script, find the entry in the _core_resource table that corresponds to your module and remove it. Then reload the page.

The installEntities() method is self-cleaning so no need to remove the database entries manually.

Appendix A: Custom Dropdowns Options

Above we mentioned that the source parameter in an attribute could be assigned to a model whose values would then populate the drop down. To do so we create a class anywhere in the MyModule/Models/ folder, for example:MyModule/Models/Product/Attribute/Source/Unit.php:

 Expand source

getAllOptions() is the only method that need be defined. It is also not necessary for this class to extendMage_Eav_Model_Entity_Attribute_Source_Abstract (though you may have some more methods to define if not ??).

Now all you do is add:

  1.     'input'     => 'select',
  2.     'source'    => 'mymodname/product_attribute_source_unit',

to the appropriate attribute in the getDefaultEntities() above.

Note, the class name must correspond to the folder layout. Additionally, MyModule_Model and mymodname must correspond to the model definition in config.xml:

 Expand source

Appendix B: Upgrade scripts

In addition to executing install scripts for each version of your module, it is also possible to have “upgrade” scripts whose filenames are of the form mysql4-upgrade-1.2.3-1.2.4.php. I’m guessing these work the same way...

Appendix C: Easier way to add options


Add the following array notation to your attribute :

'option' => array (
   'value' => array('notates_to_zero'=>array(0=>'Option Name'))

To have your model automagically add the appropriate options, there’s no need to create a model to define your options, you can do it via this script :)

Appendix D: quick func to pre-populate attribute set(s) + modification to addAttribute method


If you add this method, you can call $installer→createNewAttributeSet($name); from your script and it will pre-populate it from the default attribute set :

 Expand source


Note: ‘4’ is hard-coded as it’s the catalog/product entity_type_id, change to suit your script.

The following modification plus the addition of ‘attribute_set’ key in your setup script will make sure your custom groups don’t get added to the default or other groups:

1) Add “addAttribute” override to your Setup.php - copy the whole Method , just change the methods listed

 Expand source

Then an example snippet from the setup node :


 Expand source

Note: the above example shows also the proper functional notation for adding an option to the attribute. Then you call this method from your installer script:


NOTE: In Magento CE 1.6 using this code with the ‘option’ addition, the values were not being added to the attribute. Needed to add: Mage::app()→reinitStores to my create attribute function, but I was not creating a new attribute set. This may or may not apply to all situations, but thought I would throw it in here in case someone runs up against the same issue.

Hope this helps someone....

Appendix E - Removing Attributes


If your module has an uninstall script, you can add the following to remove the attribute if the module is uninstalled.

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->removeAttribute( 'catalog_product', 'your_added_attribute' );





  • No labels