2

So I have this custom form in the back-end from which I want to save/load values from my custom table in the database.

I have set that form to use a my custom model - this is from the system.xml file in the module:

<backend_model>myproductimporter/system_config_backend_customoptions</backend_model>

This is the class so far:

class MODULE_MyProductImporter_Model_System_Config_Backend_Customoptions extends Mage_Adminhtml_Model_System_Config_Backend_Serialized
{
    /**
     * Constructs an array of custom options data mapped to and organized by their template ID.
     * Save actual data in custom_options_import_templeates table.
     *
     * (non-PHPdoc)
     * @see Mage_Core_Model_Abstract::_beforeSave()
     */
    protected function _beforeSave()
    {
        $value = $this->getValue();
        if (is_array($value)) {
            unset($value['__empty']);
        }

        /* my custom functionality */

        $this->setValue($value);
        parent::_beforeSave();
    }

    protected function _afterLoad() {
        return parent::_afterLoad();
    }
}

So the functionality in these 2 functions is the same as in the parent class. Only the /* my custom functionality /* does some mapping stuff over the data. This manipulated data I want to save in my own way in my own table in the DB.

I have created a Resource model class:

class MODULE_MyProductImporter_Model_Resource_Import_Importer extends Mage_Core_Model_Resource_Db_Abstract {

    protected function construct() {
        $this->_init('myproductimporter/import_importer', 'id');
    }

    public function saveCustomOptions(array $valuesToSave) {
         /* process the data given from the function from Customoptions.php
            and save it in my custom table in the DB */
    }
}

And I have configured the resource class:

<global>
        <models>
            <myproductimporter>
                <class>MODULE_MyProductImporter_Model</class>
                <resourceModel>myproductimporter_resource</resourceModel>
            </myproductimporter>
            <myproductimporter_resource>
                <class>MODULE_MyhProductImporter_Model_Resource_Import_Importer</class>
                <entities>
                    <myproductimporter>
                        <table>custom_options_import_templates</table>
                    </myproductimporter>
                </entities>
            </myproductimporter_resource>
        </models>

What I don't understand is the process flow. Where should I call the saveCustomOptions function? It seems to me that it should be in the _beforeSave but that also doesn't make sense, since this function should finish its job before the save is done. I get until the end of _beforeSave(), but then I am lost in what should I do...

Also, how is the manipulated-and-ready-to-save data (in saveCustomOptions()) processed and saved?

The table where I want to save the data is called custom_options_import_templates

1

So maybe this does not answer your question here is a small insight into how magento deals with this sort of thing:

  1. Mage_Catalog_Model_Resource_Product::_afterSave - saves the links for product to website and category,
  2. Mage_Catalog_Model_Resource_Product_Option::_afterSave - saves price and and translation information for product options,
  3. Mage_Catalog_Model_Resource_Category::_beforeSave - updates path and children count,
  4. Mage_Catalog_Model_Resource_Category::_afterSave - saves product links,
  5. Mage_Cms_Model_Resource_Page::_beforeSave - validates a few attributes and sets the created date for new pages,
  6. Mage_Cms_Model_Resource_Page::_afterSave - assign pages to stores and invalidate the cache,
  7. Mage_Customer_Model_Resource_Customer::_beforeSave - validate some attributes,
  8. Mage_Customer_Model_Resource_Customer::_afterSave - trigger save of address,

So from these few items I would say, with a massive generalisation, that Magento tends to use _before save for validation and setting up some default values and used _after to saving information into linked tables.

I hope this helps you make your choice.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.