7
votes

I would like to add a new attribute to Magento's EAV attribute model. Is this possible?

I know that Magento lets you extend models using static fields (which are on the entity table), but I would like to add a new field to the EAV attribute table itself (for catalog product attributes). The new attribute will be a new setting, similar to "Visible in Category List".

1
sure it's possible just tell what you mean by static fieldAnton S
attribute as static field means a new column in the table. here i want to add a new column in the table eav_attributeuser215421
my friend you misunderstand the whole point of EAV then as it is ment to scale the field limit to row level by storing the meta information of needed attributes in different tables. To make your point please describe what value this will give and for what this will be usefulAnton S
Sorry, i dont misunderstand something ;) Magento gives to possibilities to extend a model, with static fields which are also attributes and with "normal" attributes. static fields are attributes located in the "model-table". now what i want: i want to extend the eav-attribute, not a model that use the EAV! the eav attribute should get a new setting, like "visible in cvategory list" (admin catalog manage attributes).user215421
now please edit your question to reflect the last bit of your last commentAnton S

1 Answers

12
votes

To add a new setting for product attributes, you can create an extension that (1) adds the new column to the catalog/eav_attribute table, and (2) puts in a field for the new setting in the attribute edit page using an observer.

(1) Create your new DB field (is_visible_in_category_list)

Make an SQL script for your extension, and add the new column. I would recommend using the catalog/eav_attribute table, but you could probably use eav_attribute as well:

$installer = $this;
$installer->startSetup();
$table = $installer->getTable('catalog/eav_attribute');
$installer->getConnection()->addColumn(
    $table,
    'is_visible_in_category_list',
    "TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'"
);
$installer->getConnection()->addKey(
    $table,
    'IDX_VISIBLE_IN_CATEGORY_LIST',
    'is_visible_in_category_list'
);
$installer->endSetup();

Here, I also added an index for faster querying.

(2) Add the field to the edit product attribute page

An event is fired when preparing the attribute edit form, so let's observe it:

<events>
    <adminhtml_catalog_product_attribute_edit_prepare_form>
        <observers>
            <is_visible_in_category_list_observer>
                <class>mymodule/observer</class>
                <method>addVisibleInCategoryListAttributeField</method>
            </is_visible_in_category_list_observer>
        </observers>
    </adminhtml_catalog_product_attribute_edit_prepare_form>
</events>

Then, add the new field in the observer:

public function addVisibleInCategoryListAttributeField($observer)
{
    $fieldset = $observer->getForm()->getElement('base_fieldset');
    $attribute = $observer->getAttribute();
    $fieldset->addField('is_visible_in_category_list', 'select', array(
        'name'      => 'is_visible_in_category_list',
        'label'     => Mage::helper('mymodule')->__('Visible in Category List'),
        'title'     => Mage::helper('mymodule')->__('Visible in Category List'),
        'values'    => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray(),
    ));
}

That's all. Saving the setting from the edit page is automatically handled because the field name in the form matches the DB field name.