Installing Custom Attributes with Your Module

Last modified by harikaram on Fri, June 25, 2010 09:06
Source|Old Revisions  

This is an old revision of the document!

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 into sets (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 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:

  1. <global>
  2.         <models>
  3.             <mymodname>
  4.                 <class>MyModule_Model</class>
  5.             </mymodname>
  6.         </models>
  7. ...
  8. </global>

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:

  1. <global>
  2.   <models>
  3.     <mymodname>
  4.       <class>MyModule_Model</class>
  5.     </mymodname>
  6.   </models>
  8.   <resources>
  9.     <mymodname_setup>
  10.       <setup>
  11.         <module>MyModule</module>
  12.         <class>MyModule_Model_Resource_Eav_Mysql4_Setup</class>
  13.       </setup>
  15.       <connection>
  16.         <use>core_setup</use>
  17.       </connection>
  18.     </mymodname_setup>
  20.     <mymodname_write>
  21.       <connection>
  22.         <use>core_write</use>
  23.       </connection>
  24.     </mymodname_write>
  25.     <mymodname_read>
  26.       <connection>
  27.         <use>core_read</use>
  28.       </connection>
  29.     </mymodname_read>
  30.   </resources>
  32. ...
  34. </global>

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

Step 3: Create an install script

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

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

  1. <config>
  2.     <modules>
  3.         <MyModule>
  4.             <version>1.2.3</version>
  5.         </MyModule>
  6.     </modules>
  7. ...
  8. </config>

In the file add the following code:

  1. $installer = $this;
  3. $installer->installEntities();

$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 form 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 are custom attributes. It returns a nested array defining entity types and their attributes all the attributes. A good reference is app/code/core/Mage/Catalog/Resource/Eav/Mysql4/Setup.php

  1. class MyModule_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
  2. {
  4.     /**
  5.      * @return array
  6.      */
  7.     public function getDefaultEntities()
  8.     {
  9.         return array(
  10.             'catalog_product' => array(
  11.                 'entity_model'      => 'catalog/product',
  12.                 'attribute_model'   => 'catalog/resource_eav_attribute',
  13.                 'table'             => 'catalog/product',
  14.                 'attributes'        => array(
  15.                     'myattribcode' => array(
  16.                         'group'             => 'Group/Tab',
  17.                         'label'             => 'My Attrib Label',
  18.                         'type'              => 'int',
  19.                         'input'             => 'boolean',
  20.                         'default'           => '0',
  21.                         'class'             => '',
  22.                         'backend'           => '',
  23.                         'frontend'          => '',
  24.                         'source'            => '',
  25.                         'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
  26.                         'visible'           => true,
  27.                         'required'          => false,
  28.                         'user_defined'      => false,
  29.                         'searchable'        => false,
  30.                         'filterable'        => false,
  31.                         'comparable'        => false,
  32.                         'visible_on_front'  => false,
  33.                         'visible_in_advanced_search' => false,
  34.                         'unique'            => false
  35.                     ),
  37.                    ...
  38.                )
  39.            )
  40.       );

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.

Appendix A: Custom Dropdowns Options

Rerunning the Code

If you make a change and wish to reinstall the attributes, find the entry in the _core_resource table that corresponds to your module and remove it. Then reload the page.