1

I am making a simple plugin that would save some data to DB when you click "Save Config" button in system configurations. I have added this to my system.xml

<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<backend_model>storesave/config_save</backend_model>

And in Save.php I am using _afterSave() method. I select the 'yes', but it does not run the code the first time "Save config" button is clicked. It only runs the code after I click the button second time. But when I select 'No' option and press "Save config" it reruns the code before save happens.

So I tried the _afterLoad() method, but if it was saved with 'yes' option selected, it runs the code the moment I go to my config page before I even press "Save config".

Finally I tried Using both methods to make a workaround.

public function _afterSave(){
    $this->saveState = true;
}

public function _afterLoad()
{   
    if($this->saveState == true){
        //execute code here
    }
}

But the _afterLoad() method does not trigger this way because $saveState always remains false.

So I would like to know why is this happening, and how could I make it work?

EDIT:

I use this as a check for yes No options:

   const XML_PATH_ENABLED = 'storesave/global/status';
    public function isEnabled(){
        return Mage::getStoreConfig(self::XML_PATH_ENABLED);
    }
    public function _afterSave()
    {
        if ($this->isEnabled() == true){
            //run the code
        }
    }

Even with this if selected state is Yes and I set it to No and Press "Save config", the _afterSave method still runs the code inside my if statement. So my thought is that how to make it not do that.

3

I'm assuming the default value for your option is Yes. Magento has a short-circuit mechanism to prevent most of the code in save from triggering if the data on a model is the same as the data on the model at the point it was loaded (see Mage_Core_Model_Abstract::save(). The code you place in _afterSave will therefore only get triggered if the option actually changes (which in most cases is all you really want, but your requirements may be different). As you have found the _afterLoad method will get triggered when you load the page, not just when you click save.

If your requirement is to always trigger your custom code, only when save is called, the best bet will be to simply override the save method itself.

public function save()
{
    // do your custom work here
    parent::save();
}
6
  • Yes, this works much better, but the problem remains that, when I select 'No' and press 'Save config', it still reruns the code creating duplicate data. And putting parent::save(); before my code does not change anything. – Mmazvis Mar 5 '15 at 11:32
  • Without knowing what your custom code does, it's impossible to give advice. It sounds like you just need to add protection to your custom code so that if you've already performed the action previously you don't do it again. In this case _afterSave is almost perfect for what you need with the exception that it won't get triggered the very first time due to the default value. – Peter O'Callaghan Mar 5 '15 at 11:41
  • That is the problem, that even with the _afterSave if default is Yes and I select No and press save, it still reruns the code. So that is why I asked how to prevent that. – Mmazvis Mar 5 '15 at 11:50
  • Return the code? It's really not clear what you are attempting to do. I (and I suspect others) will be unable to help further without an explanation of what your custom code is actually doing. – Peter O'Callaghan Mar 5 '15 at 11:54
  • 1
    That makes a little more sense. The call to getStoreConfig will return the value cached at the start of the request, not taking into account the change. Assuming the XML path points to the same field that has the backend_model set on it, you can use $this->getValue() to determine if it is currently yes or no. – Peter O'Callaghan Mar 5 '15 at 12:10

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.