在 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
)我收到了所有付款方式的详细列表。
最佳答案
哈,我一直在寻找理由向我的 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)。
关于自定义模块安装期间 Magento Store 配置奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8245008/