自定义模块安装期间 Magento Store 配置奇怪的行为
在 Magento 中安装自定义模块期间对商店配置的访问是否有一些限制?这是我有一个安装脚本的问题
:
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
DROP TABLE IF EXISTS {$this->getTable('userpaymentban')};
CREATE TABLE IF NOT EXISTS {$this->getTable('userpaymentban')} (
`ban_id` INT NOT NULL AUTO_INCREMENT ,
`user_id` INT NOT NULL ,
`paymentmethod_id` VARCHAR(200) NOT NULL ,
`store_id` INT NOT NULL ,
PRIMARY KEY (`ban_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;");
$defaultNotBannedPaymentMethods = array();
$paymentMethods = Mage::getSingleton('payment/config')->getAllMethods();
查询没问题,但最后一行导致抛出一些奇怪的事情:
Error in file: "/var/www/magentotest/magento161/app/code/local/Alpha/Userpaymentban/sql/userpaymentban_setup/mysql4-install-0.1.0.php" - Warning: Invalid argument supplied for foreach() in /var/www/magentotest/magento161/app/code/core/Mage/Payment/Model/Config.php on line 76
#0 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/Resource/Setup.php(645): Mage::exception('Mage_Core', 'Error in file: ...')
#1 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/Resource/Setup.php(421): Mage_Core_Model_Resource_Setup->_modifyResourceDb('install', '', '0.1.0')
#2 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/Resource/Setup.php(327): Mage_Core_Model_Resource_Setup->_installResourceDb('0.1.0')
#3 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/Resource/Setup.php(235): Mage_Core_Model_Resource_Setup->applyUpdates()
#4 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/App.php(412): Mage_Core_Model_Resource_Setup::applyAllUpdates()
#5 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/App.php(338): Mage_Core_Model_App->_initModules()
#6 /var/www/magentotest/magento161/app/Mage.php(640): Mage_Core_Model_App->run(Array)
#7 /var/www/magentotest/magento161/index.php(80): Mage::run('', 'store')
#8 {main}
异常在代码中的 Mage_Payment_Model_Config
中抛出:
public function getAllMethods($store=null)
{
$methods = array();
$config = Mage::getStoreConfig('payment', $store);
echo "<pre>";
var_dump($config);
echo "</pre>";
foreach ($config as $code => $methodConfig) {
$data = $this->_getMethod($code, $methodConfig);
if (false !== $data) {
$methods[$code] = $data;
}
}
return $methods;
}
如您所见,我已经添加了一些调试代码,但我从中收到的所有内容都只是 NULL
我的 Magento 没问题(我认为),因为当我使用Mage::getSingleton(' payment/config')->getAllMethods()
超出模块安装范围(在导入的 app/Mage.php
的文件中)我收到所有付款方式的详细列表。
Is there some restrictions of access to store configuration during installation of custom module in Magento? Here is the problem
I have an installation script:
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
DROP TABLE IF EXISTS {$this->getTable('userpaymentban')};
CREATE TABLE IF NOT EXISTS {$this->getTable('userpaymentban')} (
`ban_id` INT NOT NULL AUTO_INCREMENT ,
`user_id` INT NOT NULL ,
`paymentmethod_id` VARCHAR(200) NOT NULL ,
`store_id` INT NOT NULL ,
PRIMARY KEY (`ban_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;");
$defaultNotBannedPaymentMethods = array();
$paymentMethods = Mage::getSingleton('payment/config')->getAllMethods();
The query is OK, but last line causes of throwing some wierd thing:
Error in file: "/var/www/magentotest/magento161/app/code/local/Alpha/Userpaymentban/sql/userpaymentban_setup/mysql4-install-0.1.0.php" - Warning: Invalid argument supplied for foreach() in /var/www/magentotest/magento161/app/code/core/Mage/Payment/Model/Config.php on line 76
#0 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/Resource/Setup.php(645): Mage::exception('Mage_Core', 'Error in file: ...')
#1 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/Resource/Setup.php(421): Mage_Core_Model_Resource_Setup->_modifyResourceDb('install', '', '0.1.0')
#2 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/Resource/Setup.php(327): Mage_Core_Model_Resource_Setup->_installResourceDb('0.1.0')
#3 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/Resource/Setup.php(235): Mage_Core_Model_Resource_Setup->applyUpdates()
#4 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/App.php(412): Mage_Core_Model_Resource_Setup::applyAllUpdates()
#5 /var/www/magentotest/magento161/app/code/core/Mage/Core/Model/App.php(338): Mage_Core_Model_App->_initModules()
#6 /var/www/magentotest/magento161/app/Mage.php(640): Mage_Core_Model_App->run(Array)
#7 /var/www/magentotest/magento161/index.php(80): Mage::run('', 'store')
#8 {main}
The exception was thrown in Mage_Payment_Model_Config
in code:
public function getAllMethods($store=null)
{
$methods = array();
$config = Mage::getStoreConfig('payment', $store);
echo "<pre>";
var_dump($config);
echo "</pre>";
foreach ($config as $code => $methodConfig) {
$data = $this->_getMethod($code, $methodConfig);
if (false !== $data) {
$methods[$code] = $data;
}
}
return $methods;
}
As you can see I am already added some debugging code, but all what I receive from that is just NULL
My Magento is OK (I think) because when I am using Mage::getSingleton('payment/config')->getAllMethods()
out of module installation scope (in a file with imported app/Mage.php
) I receive a nice list of all payment methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哈,我一直在寻找理由向我的 Magento U 学生证明数据安装/升级脚本的存在,这是另一个很好的例子。
如果您查看初始化过程(从
index.php
中的Mage::run()
开始),您将很快到达Mage_Core_Model_App::run( )
。在那里您将看到对_initModules()
的调用。正是通过此方法运行“常规”安装/升级脚本(通过Mage_Core_Model_Resource_Setup::applyAllUpdates()
)。稍后在Mage_Core_Model_App::run()
中调用Mage_Core_Model_Resource_Setup::applyAllDataUpdates()
。这是运行数据安装/数据升级脚本的地方,并且是在通过_initCurrentStore()
初始化存储对象之后。这似乎就是所谓的数据脚本的目的 - 您获得已加载配置的商店对象。
这些脚本的运行/命名就像“常规”安装/升级脚本一样,唯一的区别是文件名是
mysql4-data-[install|upgrade]-[version(s)].php
对于CE < 1.6 且EE< 1.11.对于 1.6/1.11 及更高版本,数据脚本会丢失 mysql4 前缀,并放置在模块目录下的data
文件夹中(有关示例,请参阅Mage_Catalog
dir)。Ha, I'm always looking for reasons to justify the existence of data-install/upgrade scripts to my Magento U students, and this is another great example.
If you look into the initialization process (starting at
Mage::run()
inindex.php
), you'll arrive quickly atMage_Core_Model_App::run()
. In there you'll see a call to_initModules()
. It's from this method that "regular" install/upgrade scripts are run (viaMage_Core_Model_Resource_Setup::applyAllUpdates()
). Later on inMage_Core_Model_App::run()
is a call toMage_Core_Model_Resource_Setup::applyAllDataUpdates()
. This is where the data-install/data-upgrade scripts are run, and it's just after the store object is initialized via_initCurrentStore()
.That seems to be the purpose of the so-called data scripts - you get the store object with its configuration loaded.
These scripts are run/named just like "regular" install/upgrade scripts, with the only difference being that the filenames are
mysql4-data-[install|upgrade]-[version(s)].php
for CE < 1.6 and EE < 1.11. For 1.6/1.11 and up, data scripts lose the mysql4 prefix and are placed in adata
folder under your module directory (seeMage_Catalog
dir for an example).