为了将所有更改迁移到所有环境,我使用数据库升级脚本。我使用它们来创建不同的实例(客户、税务设置等),但通常用于迁移静态块和配置设置。
迁移静态块:
<?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();
?>
要迁移设置:
<?php
Mage::getModel('core/config')->saveConfig('design/theme/default', 'theme');
?>
我知道我们可以通过配置.xml修改Magento设置:
<default>
<general>
<store_information>
<name>My Store</name>
</store_information>
<content_staging>
<block_frontend_stub>home</block_frontend_stub>
</content_staging>
</general>
</default>
但据我了解,我们只有在以下情况下才能以这种方式修改设置:常规/store_information/名称和
一般/content_staging/block_frontend_stub在 db 中不存在或其值等于 NULL,如果值不是 NULL,我们无法通过 xml 修改它。我在本地环境中对其进行了测试,我认为我是对的,但在Magento上找不到负责通过xml设置配置的代码。我说的对吗?
你能告诉我负责它的代码部分吗?您对Magento的最佳迁移实践是什么?也许我不知道:)
对的,配置 xml 文件中指定的值被 core_config_data
表中的值覆盖。正如B00MER指出的那样,有问题的代码是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;
}
请注意,loadDb()
是在 loadModules()
之后调用的。
实际的合并逻辑在配置资源模型Mage_Core_Model_Resource_Config::loadToXml()
中。
对于每个全局设置,这称为:
$xmlConfig->setNode('default/' . $r['path'], $value);
对于每个网站范围设置,这称为:
$nodePath = sprintf('websites/%s/%s', $websites[$r['scope_id']]['code'], $r['path']);
$xmlConfig->setNode($nodePath, $value);
对于每个网站范围设置,这称为:
$nodePath = sprintf('stores/%s/%s', $stores[$r['scope_id']]['code'], $r['path']);
$xmlConfig->setNode($nodePath, $value);
这稍微简化了,但如果您需要更多详细信息,您可以查看源代码。
您可以在每个服务器实例上通过local.xml
指定core_config_data
设置:
<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>
来源: https://twitter.com/IvanChepurnyi/status/111544548806758403
如果您好奇Magento在哪里设置XML配置文件中的数据,请查看类:Mage_Core_Model_Config
就最佳实践而言,有很多关于这些主题的信息:
- Magento部署的最佳实践
- http://www.dhmedia.com.au/blog/perfect-magento-workflow-using-git
- Magento不应该支持模块的卸载/降级是否有理由
- http://www.aschroder.com/2010/07/structuring-your-magento-project-for-engineering-not-hacking/