自定义模块安装期间 Magento Store 配置奇怪的行为

发布于 2024-12-17 13:41:18 字数 2842 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2024-12-24 13:41:18

哈,我一直在寻找理由向我的 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() in index.php), you'll arrive quickly at Mage_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 (via Mage_Core_Model_Resource_Setup::applyAllUpdates()). Later on in Mage_Core_Model_App::run() is a call to Mage_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 a data folder under your module directory (see Mage_Catalog dir for an example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文