by Kevin Schroeder | 9:08 am

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 KeyMerged KeyDefault Value
backendbackend_model
typebackend_typevarchar
tablebackend_table
frontendfrontend_model
inputfrontend_inputtext
labelfrontend_label
frontend_classfrontend_class
sourcesource_model
requiredis_required1
user_definedis_user_defined0
defaultdefault_value
uniqueis_unique
notenote
globalis_global1 (Mage_Catalog_Model_Resource_Eav_Attribute:: SCOPE_GLOBAL)
input_rendererfrontend_input_renderer
visibleis_visible1
searchableis_searchable0
filterableis_filterable0
comparableis_comparable0
visible_on_frontis_visible_on_front0
wysiwyg_enabledis_wysiwyg_enabled0
is_html_allowed_on_frontis_html_allowed_on_front0
visible_in_advanced_searchis_visible_in_advanced_search0
filterable_in_searchis_filterable_in_search0
used_in_product_listingused_in_product_listing0
used_for_sort_byused_for_sort_by0
apply_toapply_to0
positionposition0
is_configurableis_configurable1
used_for_promo_rulesis_used_for_promo_rules0

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.

Apr 03.2013 | 01:54 pm

    kschroeder

    vinai Yep.  Did a lot of copying and pasting from there.

    Apr 03.2013 | 01:56 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.