如何在Magento中连接多个外部数据库?

时间:2022-08-31 03:01:47

I need to connect to some external databases from Magento. I found one tutorial to Create an external database connection in Magento. This tutorial was helpful and it worked for connecting to one external database. But, I have to connect more than one external databases.

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

How can I connect to more than one external databases (suppose 5 external databases) in Magento?

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

4 个解决方案

#1


2  

I found this one Magento Module that will help to connect to external database system. http://subesh.com.np/2012/02/magento-external-database-connector-v1-0-0-released/

我发现这一个Magento模块将有助于连接到外部数据库系统。 http://subesh.com.np/2012/02/magento-external-database-connector-v1-0-0-released/

I tried the module and seems to be working well.Hope this helps.

我试过这个模块,看起来效果很好。希望这有帮助。

EDIT:

编辑:

Module also available on Magento Connect. http://www.magentocommerce.com/magento-connect/sp-edb-4574.html

模块也可在Magento Connect上获得。 http://www.magentocommerce.com/magento-connect/sp-edb-4574.html

#2


5  

I haven't tested it, but I would expect that duplicating the externaldb_* nodes under global\resources with another (unique) resource name e.g. externaldb2_* should work.

我还没有对它进行过测试,但我希望将global \ resources下的externaldb_ *节点与另一个(唯一的)资源名称重复,例如: 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>

#3


4  

You can specify the resource used in the module's etc/config.xml file, so that a module will always use a certain data source or you can specify in the global config xml as described by the previous answer, then this connection will be used by default.

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

You can change the resource in your code:

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

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

#4


0  

As far as I can tell, you can't have models connecting to multiple database sources from within the same module.

据我所知,您不能让模型从同一模块中连接到多个数据库源。

What I've done, is create a parallel dummy module, that only contains the model that needs to connect to the alternate database. So the module that does all the work is in one branch, and the dummy module to talk to the other database is separate. Solves the problem beautifully, although it's not the most elegant solution... but it's not the least elegant either

我所做的是创建一个并行虚拟模块,它只包含需要连接到备用数据库的模型。因此,完成所有工作的模块位于一个分支中,与另一个数据库通信的虚拟模块是分开的。虽然它不是最优雅的解决方案,但是它可以很好地解决问题...但它也不是最不优雅的

#1


2  

I found this one Magento Module that will help to connect to external database system. http://subesh.com.np/2012/02/magento-external-database-connector-v1-0-0-released/

我发现这一个Magento模块将有助于连接到外部数据库系统。 http://subesh.com.np/2012/02/magento-external-database-connector-v1-0-0-released/

I tried the module and seems to be working well.Hope this helps.

我试过这个模块,看起来效果很好。希望这有帮助。

EDIT:

编辑:

Module also available on Magento Connect. http://www.magentocommerce.com/magento-connect/sp-edb-4574.html

模块也可在Magento Connect上获得。 http://www.magentocommerce.com/magento-connect/sp-edb-4574.html

#2


5  

I haven't tested it, but I would expect that duplicating the externaldb_* nodes under global\resources with another (unique) resource name e.g. externaldb2_* should work.

我还没有对它进行过测试,但我希望将global \ resources下的externaldb_ *节点与另一个(唯一的)资源名称重复,例如: 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>

#3


4  

You can specify the resource used in the module's etc/config.xml file, so that a module will always use a certain data source or you can specify in the global config xml as described by the previous answer, then this connection will be used by default.

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

You can change the resource in your code:

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

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

#4


0  

As far as I can tell, you can't have models connecting to multiple database sources from within the same module.

据我所知,您不能让模型从同一模块中连接到多个数据库源。

What I've done, is create a parallel dummy module, that only contains the model that needs to connect to the alternate database. So the module that does all the work is in one branch, and the dummy module to talk to the other database is separate. Solves the problem beautifully, although it's not the most elegant solution... but it's not the least elegant either

我所做的是创建一个并行虚拟模块,它只包含需要连接到备用数据库的模型。因此,完成所有工作的模块位于一个分支中,与另一个数据库通信的虚拟模块是分开的。虽然它不是最优雅的解决方案,但是它可以很好地解决问题...但它也不是最不优雅的