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

非 Y 不嫁゛ 提交于 2019-11-30 20:09:11

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

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’);
sucitivel

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!