我需要连接到Magento的一些外部数据库。我找到了一个教程 在Magento中创建外部数据库连接. 。本教程很有帮助,并且可以连接到一个外部数据库。但是,我必须连接多个外部数据库。

如何连接到Magento中的多个外部数据库(假设5个外部数据库)?

有帮助吗?

解决方案

我发现了这个洋红色模块,它将有助于连接到外部数据库系统。 http://subesh.com.np/2012/02/magento-external-database-connector-v1-0-0-relase//

我尝试了该模块,并且似乎运行良好。希望这对您有所帮助。

编辑:

Magento Connect上也可以使用模块。 http://www.magentocommerce.com/magento-connect/sp-edb-4574.html

其他提示

我还没有测试过,但是我希望这重复 externaldb_* 节点下 global\resources 使用另一个(唯一)资源名称 externaldb2_* 应该管用。

<global>
<resources>
  <externaldb_write>
    <connection>
      <use>externaldb_database</use>
    </connection>
  </externaldb_write>
  <externaldb_read>
    <connection>
      <use>externaldb_database</use>
    </connection>
  </externaldb_read>
  <externaldb_setup>
    <connection>
      <use>core_setup</use>
    </connection>
  </externaldb_setup>
  <externaldb_database>
    <connection>
      <host><![CDATA[localhost]]></host>
      <username><![CDATA[db_username]]></username>
      <password><![CDATA[db_password]]></password>
      <dbname><![CDATA[db_name]]></dbname>
      <model>mysql4</model>
      <type>pdo_mysql</type>
      <active>1</active>
    </connection>
  </externaldb_database>
  <externaldb2_write>
    <connection>
      <use>externaldb2_database</use>
    </connection>
  </externaldb2_write>
  <externaldb2_read>
    <connection>
      <use>externaldb2_database</use>
    </connection>
  </externaldb2_read>
  <externaldb2_setup>
    <connection>
      <use>core_setup</use>
    </connection>
  </externaldb2_setup>
  <externaldb2_database>
    <connection>
      <host><![CDATA[localhost2]]></host>
      <username><![CDATA[db2_username]]></username>
      <password><![CDATA[db2_password]]></password>
      <dbname><![CDATA[db2_name]]></dbname>
      <model>mysql4</model>
      <type>pdo_mysql</type>
      <active>1</active>
    </connection>
  </externaldb2_database>
</resources>

您可以指定模块的etc/config.xml文件中使用的资源,以便模块将始终使用某个数据源,或者您可以在上一个答案中所述的全局config xml中指定,然后该连接将由默认。

您可以更改代码中的资源:

$resource = Mage::getSingleton(‘core/resource’);
$conn     = $resource->getConnection(‘externaldb2_read’);

据我所知,您无法在同一模块内使用模型连接到多个数据库源。

我所做的是创建一个并行虚拟模块,该模块仅包含需要连接到备用数据库的模型。因此,完成所有工作的模块是在一个分支中,与其他数据库交谈的虚拟模块是独立的。尽管不是最优雅的解决方案,但它很好地解决了问题……但这也不是最不优雅的

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top