【问题标题】:Propel ORM Concurrent Connections推动 ORM 并发连接
【发布时间】:2012-01-19 17:35:36
【问题描述】:

我正在使用 Propel ORM 进行一个项目,需要连接到两个不同的数据库来检索某些数据。我曾尝试在线搜索指南,但没有运气,因为所有“解决方案”要么特定于 Symphony,要么根本不起作用。

我希望能够指定两个连接,并能够在某些查询期间通过将连接别名传递给查询来在它们之间切换。但是,同时打开两个连接也是可以接受的。

非常感谢任何和所有帮助!

谢谢, 迪玛

【问题讨论】:

  • 绝对有必要使用2个数据库吗?超过 1 个数据库通常是糟糕设计的结果。如果有必要,请考虑来自其中一个数据库的 Web 服务

标签: php mysql database concurrency propel


【解决方案1】:

根据Propel API Docs,您可以通过以下方式在您的 schema.xml 文件中定义数据库连接:

<?xml version="1.0" encoding="UTF-8"?>
<database name="bookstore" defaultIdMethod="native">
    <!-- table definitions go here -->
</database>

然后在您的runtime-conf.xml 设置中,您可以设置连接参数:

<datasources default="bookstore">
  <datasource id="bookstore">
    <adapter>mysql</adapter> <!-- sqlite, mysql, mssql, oracle, or pgsql -->
    <connection>
      <dsn>mysql:host=localhost;dbname=my_db_name</dsn>
      <user>my_db_user</user>
      <password>my_db_password</password>
    </connection>
  </datasource>
</datasources>

-- 编辑--

Propel 似乎不支持数据库标签的别名属性,但您应该能够摆脱以下情况:

<?xml version="1.0" encoding="UTF-8"?>
<database name="bookstore_primary" defaultIdMethod="native">
    <!-- table definitions go here -->
</database>

<database name="bookstore_secondary" defaultIdMethod="native">
    <!-- table definitions go here -->
</database>

<datasources default="bookstore_primary">
  <datasource id="bookstore_primary">
    <adapter>mysql</adapter> <!-- sqlite, mysql, mssql, oracle, or pgsql -->
    <connection>
      <dsn>mysql:host=localhost;dbname=bookstore_primary</dsn>
      <user>my_db_user</user>
      <password>my_db_password</password>
    </connection>
  </datasource>
</datasources>

<datasources default="bookstore_secondary">
  <datasource id="bookstore_secondary">
    <adapter>mysql</adapter> <!-- sqlite, mysql, mssql, oracle, or pgsql -->
    <connection>
      <dsn>mysql:host=localhost;dbname=bookstore_secondary</dsn>
      <user>my_db_user</user>
      <password>my_db_password</password>
    </connection>
  </datasource>
</datasources>

看看这是否有效。归根结底,真正重要的是可以连接到两个数据库(使用各自的 DSN)。

【讨论】:

  • 感谢您回答迈克!我使用这种方法遇到的问题是,在 runtime-conf.xml 中,数据源元素的 ID 必须与架构中的数据库名称匹配。对于我的项目,两个数据库具有相同的名称,因此如果我将它们都添加到我的 runtime-conf 中,它们将具有相同的别名...:\
  • 不,很遗憾没有。我和我的团队最终为我们想要的功能找到了一些其他的解决方法。我们正在接近我们的最后期限,并且无论实施如何都需要该功能。我相信我们会在测试版发布后回到这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
  • 2014-02-16
  • 1970-01-01
  • 2011-04-29
  • 1970-01-01
  • 2020-04-07
  • 2012-08-16
相关资源
最近更新 更多