How to connect multiple external databases in Magento?

I need to connect to some external databases from Magento. I found one tutorial for Creating an External Database Connection in Magento . This tutorial was useful and it worked to connect to one external database. But I need to connect several external databases.

How to connect to several external databases (suppose 5 external databases) in Magento?

+8
database database-connection
source share
4 answers

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

I tried the module and it seemed to work well. Hope this helps.

EDIT:

The module is also available for Magento Connect. http://www.magentocommerce.com/magento-connect/sp-edb-4574.html

+2
source share

I did not test it, but I would expect that duplication of externaldb_* nodes under global\resources with a different (unique) resource name, for example. externaldb2_* should work.

 <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> 

+5
source share

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

You can change the resource in your code:

 $resource = Mage::getSingleton('core/resource'); $conn = $resource->getConnection('externaldb2_read'); 
+4
source share

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

What I did was create a parallel dummy module that contains only the model, which should connect to an alternative database. Thus, the module that performs all the work is located in one branch, and the dummy module for communication with another database is separate. Solves the problem beautifully, although this is not the most elegant solution ... but it is not the least elegant either

0
source share

Source: https://habr.com/ru/post/650915/


All Articles