Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Static blocks and configuration settings migration

To migrate all changes to all environments I use database upgrade scripts. I use them to create different instances(customer, tax settings etc.) but usually to migrate static blocks and config settings.

To migrate static blocks:

<?php
$block = Mage::getModel('cms/block');
$data = array(
   'title' => 'Block title',
   'identifier' => 'block_identifier',
   'content' => 'block content',
   'is_active' => 1,
   'stores' => array(0 => Mage_Core_Model_App::ADMIN_STORE_ID),
);

$block->addData($data);
$block->save();
?>

To migrate settings:

<?php
Mage::getModel('core/config')->saveConfig('design/theme/default', 'theme');
?>

I know that we can modify Magento settings via config.xml:

<default>
    <general>
        <store_information>
            <name>My Store</name>
        </store_information>
        <content_staging>
            <block_frontend_stub>home</block_frontend_stub>
        </content_staging>
    </general>
</default>

But as far as I understand we can modify settings in such way only if paths: general/store_information/name and
general/content_staging/block_frontend_stub don't exists at db or their values equal NULL, if value not NULL we can't modify it via xml. I tested it on my local environment and I think I'm right but can't find a code at Magento which is responsible for setting configuration via xml. Am I right?

Can you show me the part of code which is responsible for it? And what is your best migration practices for Magento? Maybe I don't know something :)

like image 825
Viacheslav Kondratiuk Avatar asked Jun 26 '12 05:06

Viacheslav Kondratiuk


2 Answers

You are right, values specified in the config xml files are overwritten by values from the core_config_data table. As B00MER pointed out, the code in question is in Mage_Core_Model_Config::init():

public function init($options=array())
{
    $this->setCacheChecksum(null);
    $this->_cacheLoadedSections = array();
    $this->setOptions($options);
    $this->loadBase();

    $cacheLoad = $this->loadModulesCache();
    if ($cacheLoad) {
        return $this;
    }
    $this->loadModules();
    $this->loadDb();
    $this->saveCache();
    return $this;
}

Notice that loadDb() is called after loadModules().
The actual merging logic is in the config resource model Mage_Core_Model_Resource_Config::loadToXml().

For each global setting this is called:

$xmlConfig->setNode('default/' . $r['path'], $value);

For each website scope setting this is called:

$nodePath = sprintf('websites/%s/%s', $websites[$r['scope_id']]['code'], $r['path']);
$xmlConfig->setNode($nodePath, $value);

For each website scope setting this is called:

$nodePath = sprintf('stores/%s/%s', $stores[$r['scope_id']]['code'], $r['path']);
$xmlConfig->setNode($nodePath, $value);

This is slightly simplified, but if you need more detail you can look at the source.

like image 117
Vinai Avatar answered Oct 13 '22 00:10

Vinai


You can specify settings from core_config_data via local.xml on each of your server instances:

<config>
   <stores>
       <store_code>
            <!-- config value for a store  (web/unsecure/base_url)  -->
            <web>
                <unsecure>
                      <base_url>http://example-magento-store.com</base_url>
               </unsecure>
            </web>
        </store_code>
   </stores>
   <websites>
       <website_code>
            <!-- config value for a website  (web/unsecure/base_url)  -->
            <web>
                <unsecure>
                      <base_url>http://another-example-magento-store.com</base_url>
               </unsecure>
            </web>
        </website_code>
   </websites>
   <default>
      <!-- default config value (web/unsecure/base_url) -->
       <web>
            <unsecure>
                   <base_url>http://default-magento-store.com</base_url>
              </unsecure>
        </web>
   </default>
</config>

Source: https://twitter.com/IvanChepurnyi/status/111544548806758403

If your curious where Magento is setting the data from the XML configuration files look at the class: Mage_Core_Model_Config

As far as best practices, lots of info out there on the topics:

  • Best practices for Magento Deployment
  • http://www.dhmedia.com.au/blog/perfect-magento-workflow-using-git
  • is there a reason why Magento shouldn't support uninstall/downgrade for modules
  • http://www.aschroder.com/2010/07/structuring-your-magento-project-for-engineering-not-hacking/
like image 29
B00MER Avatar answered Oct 13 '22 01:10

B00MER