php - zend - 从 Controller 连接到数据库

标签 php mysql zend-framework database-connection

我有以下游戏设置:

一个数据库包含所有用户,一个数据库包含未完成教程的用户。在第一个数据库中,我有一个标志,告诉我用户“Gogu”是否完成了教程。如果他没有,我需要连接到第二个数据库并获取一些数据。经过一些研究,我发现了这个:connecting to two different databases with Zend Framework .

问题是,因为只有大约 5% 的用户会在教程进度中保持两个连接是没有用的,所以我只需要在 Controller 中连接,获取我需要的东西并关闭连接。

知道怎么做吗?

最佳答案

您不必担心 2 个连接,因为 Zend_Db“延迟加载”连接。来自 ZF 手册:

Creating an instance of an Adapter class does not immediately connect to the RDBMS server. The Adapter saves the connection parameters, and makes the actual connection on demand, the first time you need to execute a query. This ensures that creating an Adapter object is quick and inexpensive. You can create an instance of an Adapter even if you are not certain that you need to run any database queries during the current request your application is serving.

http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.connecting.getconnection

请注意 accepted answer 中使用的方法您从 Bootstrap 中引用了 $db->getConnection() 调用。不推荐这样做,因为它会破坏延迟加载的目的。您还可以考虑 Zend_Application_Resource_Multidb,这可能是一种更优雅的方法:

http://framework.zend.com/manual/en/zend.application.available-resources.html#zend.application.available-resources.multidb

关于php - zend - 从 Controller 连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11306122/

相关文章:

php - 获取带有点赞和评论的帖子 - 一个查询?

php - 无法连接到 MySQL - 断电后丢失东西?

php - Zend 自动加载器 : custom library in Module-Folder

php - 如何从 php/Zend 为 LDAP 使用多个 TLS 证书?

php - 为什么我的单元测试通过 Hudson/Phing 失败,但通过命令行上的 Phing 成功?

日期之间的sql查询中的php数组

php - phpmyadmin 中一列与另一列的总和

php - MySQL同时select两个表

mysql - 何时使用 ON DELETE CASCADE

mysql - 如何将 sql.gz 文件加载到我的数据库中? (输入)