我有以下游戏设置:
一个数据库包含所有用户,一个数据库包含未完成教程的用户。在第一个数据库中,我有一个标志,告诉我用户“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,这可能是一种更优雅的方法:
关于php - zend - 从 Controller 连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11306122/