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 our custom attributes. It returns a nested array defining entity types and their 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.            ...  // define attributes for other model entities here
  41.       );

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:

class Validation class for data. Example values: validate-digits, validate-number
backend Model for handling input in the backend (eg customer/customer_attribute_backend_password)
frontend Model for handling display of attribute on the frontend (eg catalog/product_attribute_frontend_image)
source Model for values from which to choose in the backend. Used for select boxes/dropdowns

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:

  1. class MyModule_Model_Product_Attribute_Source_Unit extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
  2. {
  3.     public function getAllOptions()
  4.     {
  5.         if (!$this->_options) {
  6.             $this->_options = array(
  7.                 array(
  8.                     'value' => '',
  9.                     'label' => '',
  10.                 ),
  11.                 array(
  12.                     'value' => '1',
  13.                     'label' => 'Day',
  14.                 ),
  15.                 array(
  16.                     'value' => '2',
  17.                     'label' => 'Week',
  18.                 ),
  19.                 array(
  20.                     'value' => '3',
  21.                     'label' => 'Month',
  22.                 ),
  23.                 array(
  24.                     'value' => '4',
  25.                     'label' => 'Year',
  26.                 )
  27.             );
  28.         }
  29.         return $this->_options;
  30.     }
  31. }

getAllOptions() is the only attribute that need be defined. It is NOT necessary for this class to extend Mage_Eav_Model_Entity_Attribute_Source_Abstract.

Now all you do is add:

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

to the appropriate attribute.

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

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

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...