EAV Properties for Magento

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.

[table id=2 /]

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.

$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',


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

2 Thoughts to “EAV Properties for Magento”

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

Leave a Reply

Your email address will not be published.