如何在Magento中动态加载配置参数?

时间:2014-12-05 17:05:40

标签: magento dynamic configuration system external

我在Magento中有一个自定义模块,其中包含一些系统配置字段,连接的经典数据库参数(主机,用户,传递,数据库名称)。 我成功连接到外部数据库(link),但我必须使用静态参数,如:

<phplist_database>
    <connection>
        <host><![CDATA[localhost]]></host>
        <username><![CDATA[root]]></username>
        <password><![CDATA[root]]></password>
        <dbname><![CDATA[phplist]]></dbname>
        <model>mysql4</model>
        <type>pdo_mysql</type>
        <active>1</active>
    </connection>

如何从我的system \ config参数加载该数据库参数连接?

2 个答案:

答案 0 :(得分:0)

<?php
    $app = Mage::app('default');
    $config  = Mage::getConfig()->getResourceConnectionConfig("default_setup");
    $dbinfo = array('host' => $config->host,
                'user' => $config->username,
                'pass' => $config->password,
                'dbname' => $config->dbname
    );

    $hostname = $dbinfo['host'];
    $user     = $dbinfo['user'];
    $password = $dbinfo['pass'];
    $bank     = $dbinfo['dbname'];
?>

答案 1 :(得分:0)

您可以通过资源即时设置数据库连接。

$resource = Mage::getSingleton('core/resource');
$resource->createConnection('phplist_database', 'pdo_mysql', array(
    'host'     => Mage::helper('config')->getHost(),
    'username' => Mage::helper('config')->getUsername(),
    'password' => Mage::helper('config')->getPassword(),
    'dbname'   => Mage::helper('config')->getDbName(),
    'initStatements' => 'SET NAMES utf8',
    'type'     => 'pdo_mysql',
    'model'    => 'mysql4',
    'active'   => '1',
    'charset'  => 'utf8'
));