Installing Custom Attributes with Your Module
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...)
The following steps can be taken before OR after the module has been installed into Mage.
Create the folder Resource/Eav/Mysql4/ in your module folder. Create a file called Setup.php and make a class definition as follows:
- class MyModule_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
...where MyModule_Model is what is defined as the base model in config.xml:
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...
Add a <resource> block under <global> in the module’s etc/config.xml file:
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.
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
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.