views:

33

answers:

1

Hi

How can I override the way magento saves and load a specific attribute? I want to create an attribute that implements a many-to-many relation to products so I have a custom table with entity_id and attr_id. I'm using a multiselect attribute but magento saves the values as csv in a text field. I want to override this functionality and save and load from the m2m table.

When defining a backend model for an attribute I only have control over beforeSave and afterSave but not on save itself.

Thanks

A: 

I think you're going to have to create a custom module with your own Model that extends Mage_Core_Model_Abstract. That way you can override the save() function (always making sure that you call parent::save() in there too!) to perform your multi-table logic.

Once you've created that model, can you create a product attribute that uses the Model as a frontend_model and/or backend_model. You can create the attribute in the config.xml of your module or in the mysql setup.

This is not trivial, and be careful with overriding the save() method, that's getting in deep. Good idea to setup some unit tests to make sure you're not inadvertently breaking things...

Jonathan Day
I already built a custom module with a custom attribute. I defined a backend and source models for this attribute. I couldn't find a save method in Mage_Eav_Model_Entity_Attribute_Backend_Abstract, Mage_Eav_Model_Entity_Attribute_Source_Abstract or Mage_Eav_Model_Entity_Attribute_Frontend_Abstract. What attribute model calls or extends Mage_Core_Model_Abstract::save()?
pablo
@Jonathan, you can't override Mage_Core_Model_Abstract via a Module. The only classes Magento allows you to override are classes you can instantiate, and you can't instantiate an abstract class. This is a limitation of userspace PHP.
Alan Storm
@Alan, true, I worded my answer poorly. I meant that by creating your own module, you can define `save()` within your `Namespace_Module_Model_Model` that extends `Mage_Core_Model_Abstract` thereby overriding the abstract `save()`. Doesn't that achieve the same effect?
Jonathan Day
@Pablo, if you create a Model that extends `Mage_Core_Model_Abstract` and have your backend and source objects call that Model for their data, that could work?Also, have a look at `Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Attribute_Backend_Tierprice` for an interesting example. It extends `Mage_Core_Model_Mysql4_Abstract` which provides the opportunity to override `save()`
Jonathan Day
Ah, I misread what you said. I thought you were recommending overriding the abstract class's save. Re-reading it, its clear you're recommending that pablo override the save method of whatever model contains the attribute.
Alan Storm