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...)
The following steps can be taken before OR after the module has been installed into Mage.
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:
- 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. 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>.
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
In the file add the following code:
$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...
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.
- 'additional_attribute_table' => 'catalog/eav_attribute',
- 'entity_attribute_collection' => 'catalog/product_attribute_collection',
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:
|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 defining values for select boxes/dropdowns (see below)|
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.
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:
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:
- 'input' => 'select',
- '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:
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...
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 :)
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 :
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
Then an example snippet from the setup node :
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....
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' );