开发者

How to connect multiple external databases in Magento? [closed]

开发者 https://www.devze.com 2023-02-03 08:12 出处:网络
Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 10 years ago.

Improve this question

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.

How can I connect to more than one external databases (suppose 5 extern开发者_如何学编程al databases) in Magento?


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


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.

You can change the resource in your code:

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


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/

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号