我正在以编程方式创建网站/用户等......
问题是:创建网站时,我无法立即设置配置值。
Code:
<?php
/* Website information */
$website_data = array(
'name' => 'Company name',
'code' => 'website_company_1',
'sort_order' => '1',
);
/* Save website */
$website = Mage::getModel('core/website');
$website->setData($website_data);
$website->save();
/* Get website code */
$web_code = $website->getCode();
/* R-int stores */
Mage::app()->reinitStores();
/* Config data array example */
$data = array('name' => 'Company 1', 'phone' => '056 22 33 61')
/* Set config values in array */
$groups = array();
foreach($data as $key => $value){
$groups['store_information']['fields'][$key]['value'] = $value;
}
/* Save config values */
Mage::getModel('adminhtml/config_data')
->setSection('general')
->setWebsite($web_code)
->setStore(NULL)
->setGroups($groups)
->save();
/* Re-init again */
Mage::app()->reinitStores();
但是,由于某种原因,这不起作用,但如果我先创建一个网站(使用相同的代码),然后执行此配置保存功能,它就可以正常工作。好像它需要先加载新页面才能设置/更新配置值。我以为重新初始化可以解决这个问题,但事实并非如此......
想法?
为此,您应该使用安装/升级脚本(这些是模块内的脚本)sql文件夹)。您甚至可能希望创建一个特定于设置的模块来运行这些模块。
只需在模块的全局/资源节点中声明一个设置资源,然后创建完成此操作所需的文件。使用Mage_Core_Model_Resource_Setup http://svn.magentocommerce.com/source/branches/1.5/app/code/core/Mage/Core/Model/Resource/Setup.php或者让你的设置类从那里扩展。
See Mage_Core_Model_Resource_Setup::设置配置数据() and Mage_Core_Model_Resource_Setup::删除配置数据().
Mage_Core_Model_Resource_Setup::添加配置字段()也可以使用,但据我所知并未在核心中实现。
<?xml version="1.0" ?>
<!-- module config.xml -->
<config>
<modules>
<Your_Module>
<version>1.0</version>
<!-- upgrade script #s evaluated with version_compare(), FYI -->
</Your_Module>
</modules>
<global>
<resources>
<unique_node>
<setup>
<!-- match node under <modules> -->
<module>Your_Module</module>
<class>Mage_Core_Model_Resource_Setup</class>
</setup>
</unique_node>
</resources>
</global>
</config>
然后在您的安装/升级脚本中执行以下操作:
<?php
$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */ //or whatever you configured
$installer->startSetup();
$installer->setConfigData($path, $value, $scope='default', $scopeId=0) //inherit is not implemented
$installer->endSetup();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)