The only thing I hate more than bad code completion is no code completion. When working with PHP arrays for configuration there are often options that you need to remember to properly configure the object, factory or whatever, that you are using.
What I really like to do for my own code is that when I do have some kind of array-based configuration I like to have class constants that define what options there are for me to set. Because they are constants they cut down on fat-finger problems and also take out a lot of the guesswork.
Magento is pretty good about doing this, but one place where I have not found much by way of constants is in the EAV system. There are a number of EAV properties that you can set. Here is a table of the ones I found.
Config Key | Merged Key | Default Value |
---|---|---|
backend | backend_model | |
type | backend_type | varchar |
table | backend_table | |
frontend | frontend_model | |
input | frontend_input | text |
label | frontend_label | |
frontend_class | frontend_class | |
source | source_model | |
required | is_required | 1 |
user_defined | is_user_defined | 0 |
default | default_value | |
unique | is_unique | |
note | note | |
global | is_global | 1 (Mage_Catalog_Model_Resource_Eav_Attribute:: SCOPE_GLOBAL) |
input_renderer | frontend_input_renderer | |
visible | is_visible | 1 |
searchable | is_searchable | 0 |
filterable | is_filterable | 0 |
comparable | is_comparable | 0 |
visible_on_front | is_visible_on_front | 0 |
wysiwyg_enabled | is_wysiwyg_enabled | 0 |
is_html_allowed_on_front | is_html_allowed_on_front | 0 |
visible_in_advanced_search | is_visible_in_advanced_search | 0 |
filterable_in_search | is_filterable_in_search | 0 |
used_in_product_listing | used_in_product_listing | 0 |
used_for_sort_by | used_for_sort_by | 0 |
apply_to | apply_to | 0 |
position | position | 0 |
is_configurable | is_configurable | 1 |
used_for_promo_rules | is_used_for_promo_rules | 0 |
Kind of a lot. And as you get more experience you will memorize them. But what a pain in the butt.
So I created a new GitHub repository that allows you to simply use some class constants instead of hand-typed array keys for EAV configuration. Most of the code I have seen for EAV configuration uses inputted key values instead of constants and my Google searches have not yielded much. So my presumption, possibly incorrect, is that this kind of helper class does not exist.
Using the class is very, very simple. You only need to put it in code from GitHub in code/local/Eschrade/Util. The autoloader should take care of the rest. No need to manage a config file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $installer = Mage::getResourceModel('catalog/setup','default_setup'); $config = array( Eschrade_Util_Statics::EAV_TYPE_ALIAS => Eschrade_Util_Statics::EAV_TYPE_TEXT, Eschrade_Util_Statics::EAV_INPUT_ALIAS => Eschrade_Util_Statics::EAV_INPUT_BOOLEAN, Eschrade_Util_Statics::EAV_LABEL_ALIAS => 'Some Attribute', ); $installer->addAttribute( Mage_Catalog_Model_Product::ENTITY, 'some_attribute', $config ); $installer->updateAttribute( Mage_Catalog_Model_Product::ENTITY, 'some_attribute', Eschrade_Util_Statics::EAV_LABEL, 'Some Other Attribute' ); |
Note that for the addAttribute() call I use the name + _ALIAS. So ‘label’ for adding an attribute is Eschrade_Util_Statics::EAV_LABEL_ALIAS but for updating an attribute it is Eschrade_Util_Statics::EAV_LABEL.
Also note that this class is not complete. I will make additions to it as I have time and I hope that others who see holes make push requests to add more things.
Please, please fork it, make additions and fix errors you may see. Personally, I would rather be able to choose from a list of options than having to manually type things in. It makes things much cleaner and predictable. So I hope that this will make things a little easier for you.
Comments
vinai
Cool idea!
For the default values I find it useful to look into the $installer class definition Mage_Catalog_Model_Resource_Setup and it’s super class Mage_Eav_Model_Entity_Setup, in the method _prepareValues().
With a proper phpdoc type hint /** @var $installer Mage_Catalog_Model_Resource_Setup */ that’s only a click away.
kschroeder
vinai Yep. Did a lot of copying and pasting from there.