Magento如何在管理面板中保存自定义字段

Magento如何在管理面板中保存自定义字段

问题描述:

In Manage Products -> Edit a product -> Images I've added 2 new columns (which are located in the catalog_product_entity_media_gallery_value table).

Those 2 columns are filled every time there is a media import and each field only show the value (you can't enter a new value). It's also used on the frontend. I've modified in my own theme the correct admin template file and created a new product.js to display the 2 new fields. So far everything is working as intended.

But whenever a user tries to modify something in the Images section, let say the label of an image or the position, my 2 custom fields are not saved and Magento give them the NULL value.

Because the 2 new columns are not input fields, I thought Magento would not update it, but looks like it does. I've been trying to find where the label and position are saved for the Images section so that I can add my custom values but I can't find it. Could somebody point me in the right direction?

Or would it be better to listen to the catalog_product_save_after event and create an observer to save my custom values?

Any help would be great, thank you.

Finally found it, I missed it during my previous searches. I'll answer it myself just in case it can help someone else. Code is in the Mage_Catalog_Model_Product_Attribute_Backend_Media class, inside the afterSave function. All you have to do is to rewrite it.

Config.xml should contain the following :

<models>
    <catalog_resource>
        <rewrite>
            <product_attribute_backend_media>
                Namespace_Module_Model_Resource_Product_Attribute_Backend_Media
            </product_attribute_backend_media>
        </rewrite>
    </catalog_resource>
</models>

And class should be : Namespace_Module_Model_Product_Attribute_Backend_Media extends Mage_Catalog_Model_Product_Attribute_Backend_Media