zend - 从控制器连接到数据库

时间:2022-10-09 22:58:14

I have the following setup for a game:

我有一个游戏的以下设置:

One database with all users, one database with users that didn't complete the tutorial. In the first database I have a flag that tells me if user "Gogu" completed the tutorial or not. If he didn't, I need to connect to the second database and get some data. After some research I found this: connecting to two different databases with Zend Framework.

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

The thing is that since only like 5% of the users will be in tutorial progress is no use to keep both connections so I only need to connect while in a controller, get what I need and close the connection.

问题是,因为只有5%的用户会在教程进度中没有用来保持两个连接所以我只需要在控制器中连接,得到我需要的东西并关闭连接。

Any idea how to do this?

知道怎么做吗?

1 个解决方案

#1


3  

You don't have to worry about 2 connections, because Zend_Db "lazy loads" the connection. From the ZF manual:

您不必担心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.

创建Adapter类的实例不会立即连接到RDBMS服务器。适配器保存连接参数,并在您第一次执行查询时按需进行实际连接。这可确保快速且廉价地创建Adapter对象。即使您不确定在应用程序提供的当前请求期间是否需要运行任何数据库查询,也可以创建适配器实例。

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

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

Just note that the method used in the accepted answer you referred calls $db->getConnection() from the bootstrap. This not recommended because it will defeat the purpose of lazy loading. You could also consider Zend_Application_Resource_Multidb, which is perhaps a more elegant approach:

请注意,您引用的已接受答案中使用的方法从引导程序调用$ db-> getConnection()。这不推荐,因为它会破坏延迟加载的目的。您还可以考虑Zend_Application_Resource_Multidb,这可能是一种更优雅的方法:

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

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

#1


3  

You don't have to worry about 2 connections, because Zend_Db "lazy loads" the connection. From the ZF manual:

您不必担心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.

创建Adapter类的实例不会立即连接到RDBMS服务器。适配器保存连接参数,并在您第一次执行查询时按需进行实际连接。这可确保快速且廉价地创建Adapter对象。即使您不确定在应用程序提供的当前请求期间是否需要运行任何数据库查询,也可以创建适配器实例。

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

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

Just note that the method used in the accepted answer you referred calls $db->getConnection() from the bootstrap. This not recommended because it will defeat the purpose of lazy loading. You could also consider Zend_Application_Resource_Multidb, which is perhaps a more elegant approach:

请注意,您引用的已接受答案中使用的方法从引导程序调用$ db-> getConnection()。这不推荐,因为它会破坏延迟加载的目的。您还可以考虑Zend_Application_Resource_Multidb,这可能是一种更优雅的方法:

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

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