3

I have a question related to core_config_data. Need some magento experts to clarify this.

For eg:

I have paypal extension disabled by default. When you enable it from the admin interface it enables the extension and creates quite few settings in the core_config_data table.

I want to enable the paypal extension using a data script in a custom extension. I am using the following code to do so

    <?php
     $installer = $this;
     $installer->startSetup();
     $setup = new Mage_Core_Model_Config();
     $setup->saveConfig('payment/paypal_express/active', '1', 'default', 0);
     $installer->endSetup();
    ?>

But when I run this in new environment, it only saves the one setting (payment/paypal_express/active) in the core_config_data table. The rest of the settings are not in the core_config_data. How do I trigger the creation of the remaning settings through my extension.

1
  • What are the remaining settings?
    – mbalparda
    Commented Sep 8, 2015 at 15:57

2 Answers 2

3

Bit of a minor detail but I think best practice would be to use a data installer for this instead of a sql installer.

But, to the point: you'll have to set each value yourself. There is very little magic behind setting the config values. There is no automation. So simply make sure you add all paths with values.

app/code/(community|local)/[Namespace]/[Module]/data/[namespace]_[module]_setup/data-install-[version].php

$config = array(
    array(
        'path' => 'payment/paypal_express/active',
        'value' => '1',
    ),
    array(
        'path' => 'some/custom/config_value',
        'value' => '1',
    ),
);

foreach ($config as $item) {
  $setup = new Mage_Core_Model_Config();
  $setup->saveConfig($item['path'], $item['value'], 'default', 0);
}
4
  • Just to clarify lets say I want to enable the paypal module which sets 85 config values in the core_config_data. I would to have add all 85 configs in my data installer script. Isn't that an overkill ? is there a way to extend the paypal module and add/append/extend their data installer script ? Commented Sep 8, 2015 at 17:11
  • In the config.xml of the Paypal module there will be default settings in the default tag that you can fall back on, so you don't need to set those. But any value that should be different from whats in the default values has to be set. Commented Sep 8, 2015 at 17:14
  • that was very useful info. Thanks for that. Commented Sep 8, 2015 at 17:35
  • You're welcome. If it solved your answer don't forget to accept the answer :) Commented Sep 8, 2015 at 17:41
4

How it works

The settings that you're referring to are set when you save the admin panel view of the System Config.

However, these will not save if the values are set to be "inherit from parent" or from an outer scope such as Website or global.

XML configuration in the module's etc/config.xml are the final fallback and Magento will load these if it finds nothing else saved in core_config_data.

The problem

You're saving merely one key. If you take a look at the form POST from saving it in the db, we're sending all possible values.

Solving

Sander's solution is correct, you want to provide the full range of settings. But my suggestion is that, unless you're overriding the default values, only values that are changed will need to be inserted into the config.

Therefore, let it fall back to the XML config as much as possible and insert only the values you need into the db config. Try to avoid calling saveConfig in a loop as this may cause some overhead on the db.

To do this you can save individual keys, as you're doing, or en masse. Magento does this by saving structured arrays as "groups". Take a look at how they handle it in Mage_Adminhtml_System_ConfigController::saveAction:

$section = $this->getRequest()->getParam('section');
$website = $this->getRequest()->getParam('website');
$store   = $this->getRequest()->getParam('store');
Mage::getSingleton('adminhtml/config_data')
    ->setSection($section)
    ->setWebsite($website)
    ->setStore($store)
    ->setGroups($groups)
    ->save();

One problem I see here, though, is that saving the config doesn't necessarily mark the old config as invalidated though. This may create a problem. That admin controller goes to the trouble itself of reinit'ing the config and calling a dispatch:

// reinit configuration
Mage::getConfig()->reinit();
Mage::dispatchEvent('admin_system_config_section_save_after', array(
    'website' => $website,
    'store'   => $store,
    'section' => $section
));
Mage::app()->reinitStores();

Now, this may not apply to you or what you're doing, but it may be something to be aware of.

Best of luck!

1
  • I am setting more than one configs, these are paypal api username, paypal api password, paypal api signature and paypal sandbox flag. I am overriding the default values since some of these are empty and some are turned off. I am calling the saveConfig mulitple times and not in a loop. Thanks for the explanation Commented Sep 8, 2015 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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