Magento:设置刚刚创建的网站的配置值?
我正在以编程方式创建网站/用户等...
问题如下:创建网站时,我无法立即设置配置值。
代码:
<?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();
但是,由于某种原因,这不起作用,但如果我先创建一个网站(使用相同的代码),然后执行此配置保存功能,它就可以正常工作。好像它需要先加载新页面才能设置/更新配置值。我以为重新初始化可以解决这个问题,但事实并非如此……
有什么想法吗?
I'm programmtically creating websites/users etc ...
Here's the problem: When creating a website, I can't immediatly set the config values afterwards.
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();
However, this doesn't work for some reason, but if I create a website first(with the same code), then execute this config-save function afterwards, it works fine. As if it needs a new page load first before it can set/update config values. I thought the re-init would solve this but it doesn't ...
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您应该使用安装/升级脚本(这些是模块的 sql 文件夹内的脚本)。您甚至可能希望创建一个特定于设置的模块来运行这些模块。
只需在模块的全局/资源节点中声明一个设置资源,然后创建完成此操作所需的文件。使用 Mage_Core_Model_Resource_Setup 或者让您的设置类从那里扩展。
请参阅 Mage_Core_Model_Resource_Setup::setConfigData() 和 Mage_Core_Model_Resource_Setup::deleteConfigData()。
Mage_Core_Model_Resource_Setup::addConfigField() 也可以使用,但据我所知,它没有在核心中实现。
然后在您的安装/升级脚本中执行以下操作:
You should use install/upgrade scripts for this purpose (these are the scripts inside modules' sql folders). You may even wish to create a setup-specific module with/in which to run these.
Simply declare a setup resource in your module's global/resources node and then create the file(s) you need to accomplish this. Use Mage_Core_Model_Resource_Setup or have your setup class extend from there.
See Mage_Core_Model_Resource_Setup::setConfigData() and Mage_Core_Model_Resource_Setup::deleteConfigData().
Mage_Core_Model_Resource_Setup::addConfigField() could also be used but is not implemented in the core from what I can tell.
Then in your install/upgrade scripts do this:
您没有提到您使用的是哪个 Magento 版本。我已经在 1.4.1.1 上测试了以下内容并进行了一些更改,因此它是一个正在运行的示例。
。
缓存
主要区别在于重新加载配置的更改,同时也重新加载
完整示例:
You didn't mention which Magento version you are on. I have tested the below on 1.4.1.1 and made some changes so it is a running example.
The main difference is the change from
to
which re-loads the config while also reloading the cache.
Complete Example: