Chamal Chamikara
Chamal Chamikara

Reputation: 496

Magento how to detect changed fields only in admin section?

I need to insert some values to custom database table based on the values of changed custom field, if the specific custom field value (in a custom shipping method) had changed.I need to check this in my Observer.php event that I'm firing is admin_system_config_changed_section_carriers for getting values from the field and insert values to the table

is there any possible way to do this ?

EDIT:

here is my observer function

public function handle_adminSystemConfigChangedSection($observer){

        $post = Mage::app()->getRequest()->getPost();
        $firstBarcodeFlatrate = $post['groups']['flatrate']['fields']['barcode_start']['value'];
        $lastBarcodeFlatrate = $post['groups']['flatrate']['fields']['barcode_end']['value'];

        $flatrateRange = range($firstBarcodeFlatrate,$lastBarcodeFlatrate);
        $shippingMethod = 'flatrate';

        foreach($flatrateRange as $barcodes){
            $insertData = array(
                'barcode' => $barcodes,'shipping_method' => $shippingMethod,'status' => 1,
            );
            $model = Mage::getModel('customshippingmethods/customshippingmethods')->setData($insertData);
            try {
                $model->save();
            } catch (Exception $e){
                echo $e->getMessage();
            }
        }

as you can see above database query will update each time I save the configuration but I just need to run the query iff $firstBarcodeFlatrate value had changed

Upvotes: 0

Views: 1561

Answers (1)

zokibtmkd
zokibtmkd

Reputation: 2183

I would probably go with two options: 1. Cache the last value of $firstBarcodeFlatrate $cacheId = 'first_barcode_flatrate_last_value'; $cache = Mage::app()->getCache(); $lastValue = $cache->load($cacheId); if (!$lastValue) { //We don't have cached last value, we need to run the query and cache it: //Cache the value: $cache->save($firstBarcodeFlatrate, $cacheId, array('first_barcode_flatrate_last_value'), null); //Run the query here } else { //We have last value, we need to check if it has been changed: if($lastValue != $firstBarcodeFlatrate) { //Update the cached value: $cache->save($firstBarcodeFlatrate, $cacheId, array('first_barcode_flatrate_last_value'), null); //Run the query here. } } Option 2 is to create another table with a single row and two fields or add another system config field that will store the last used value. Then before the running the query, you will check this value if it's different than $firstBarcodeFlatrate you will run the query, otherwise you won't, though I think the caching will do the job for you.

Upvotes: 2

Related Questions