【问题标题】:In Magento how do you get the database name?在 Magento 中如何获取数据库名称?
【发布时间】:2011-06-04 14:56:43
【问题描述】:

如何从 Magento 获取数据库名称?

我见过像下面这样可以获取表名的函数。

$orderItemTable = Mage::getSingleton('core/resource')->getTableName('sales/order_item');

我希望有这样的功能:

Mage::getSingleton('core/resource')->getDatabaseName();

提前感谢您的任何想法。

【问题讨论】:

    标签: database magento


    【解决方案1】:

    每个模块都可以指定自己的连接,因此您可以通过模型进行操作。

    $config = Mage::getResourceModel('sales/order')->getConnection()->getConfig();
    // $config is an array
    $dbname = $config['dbname'];
    

    如果您只对默认设置感兴趣,那么更有效的方法可能是:

    $dbname = (string)Mage::getConfig()->getNode('global/resources/default_setup/connection/dbname');
    

    【讨论】:

    • 该模型是我想走的路,但是当我尝试上述方法时出现以下错误。致命错误:调用未定义的方法 Mage_Sales_Model_Mysql4_Order::getConnection()
    • 改用getReadConnectiongetWriteConnection 试试。
    • 就是这样,仅供阅读此更改的任何人将来参考 $config = Mage::getResourceModel('sales/order')->getConnection()->getConfig(); to Mage::getResourceModel('sales/order')->getReadConnection()->getConfig();
    【解决方案2】:

    获取数据库名称尝试

    Mage::getConfig()->getResourceConnectionConfig('default_setup')->dbname;
    

    How to get magento database details

    【讨论】:

      【解决方案3】:

      总是可以从 Mage::getSingleton('core/resource')->getConnection('core_write') 获取当前连接,特别是如果您只使用一个数据库。

      $configArray = Mage::getSingleton('core/resource')->getConnection('core_write')->getConfig();
      $db = $configArray['name'];
      

      但它带有一个 zend 适配器 Zend_Config

      // Create the object-oriented wrapper upon the configuration data
      $config = new Zend_Config($configArray);
      $db = $config->get('dbname');
      

      【讨论】:

        猜你喜欢
        • 2012-03-08
        • 2014-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 2012-09-27
        • 1970-01-01
        相关资源
        最近更新 更多