Question

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.

Was it helpful?

Solution

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);
}

OTHER TIPS

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top