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

发布于 2024-10-11 12:23:24 字数 1459 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风吹雨成花 2024-10-18 12:23:24

我还没有测试过它,但我希望将 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>

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>

不气馁 2024-10-18 12:23:24

您可以在模块的 etc/config.xml 文件中指定使用的资源,以便模块始终使用某个数据源,或者您可以按照前面的答案在全局配置 xml 中指定,然后该连接将被使用默认。

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

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

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 2024-10-18 12:23:24

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

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

编辑:

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

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

森林很绿却致人迷途 2024-10-18 12:23:24

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

我所做的是创建一个并行虚拟模块,该模块仅包含需要连接到备用数据库的模型。因此,完成所有工作的模块位于一个分支中,而与其他数据库通信的虚拟模块是单独的。完美地解决了问题,虽然它不是最优雅的解决方案......但它也不是最不优雅的

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文