与 Zend 的动态数据库连接
我试图实现的结果涉及拥有一个带有数据库的应用程序,该数据库包含最少的表,用于存储系统上用户的数据,该数据库将用于在用户登录应用程序时验证用户的登录详细信息。我想要做的是在用户登录后根据主数据库中存储的有关该用户的详细信息动态连接到另一个数据库。
基本上,我希望系统上的每个用户都有自己的数据库,登录后连接到该数据库,并且能够使用常见的数据库模型类(因为每个用户的数据库将具有与每个其他用户的数据库相同的表)。
这可能吗?如果是这样,我将如何实现动态数据库连接(假设在验证其登录详细信息的操作中)。
任何帮助将不胜感激。
The result I'm trying to achieve involves having an application with a database with minimal tables that store data regarding users on the system, this database would be used to verify user's login details when they login to the application. What I want to be able to do is to dynamically connect to another database once the user has logged in, based on details stored about that user in the main database.
Basically I want each user on the system to have their own database, connect to that database once they login, and be able to use common db model classes (since each user's db will have the same tables as every other user's db).
Is this possible? And if so, how would I implement the dynamic db connection (let's say in the action that validates their login details).
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的答案是不建立第二个连接,而只需在确定给定用户所需的名称后更改默认架构即可。
例如,获取给定用户的辅助数据库的名称:
然后运行查询来更改架构。请注意,
USE
语句不支持成为准备好的语句,因此您必须使用驱动程序 API 来执行它:现在,如果您引用一个表而不限定其架构,您将在二级数据库。这包括您通过 Zend_Db 适配器或表类等运行的任何查询。
但是,仅当辅助数据库与包含用户详细信息的主数据库驻留在同一数据库实例上时,
USE
语句才有效。而且这还假设您不需要根据数据库重新验证用户身份,因为每个用户都有不同的数据库级凭据。The simplest answer is not to make a second connection, but just change the default schema after you determine the name that the given user needs.
For example, fetch the name of the secondary db for a given user:
Then run a query to change the schema. Note the
USE
statement does not support being a prepared statement, so you have to execute it using the driver API:Now if you reference a table without qualifying its schema, you'll get the table instance in the secondary database. This includes any query you run via the Zend_Db adapter or Table classes, etc.
But the
USE
statement only works if your secondary databases reside on the same database instance as your primary database with the user details. And also this assumes you don't need to re-authenticate the user against the database because each user has different database-level credentials.当然有可能。实现实际上取决于比您给出的更具体的要求,但我可能会制作某种
Db_Manager
类。抽象出所有细节以保持操作简短且简洁...那么您可能只需执行如下操作:这样您就可以处理要使用的数据库的所有实际排序并将其传播到管理器类中的模型。如果出现这种情况,它还可以为您提供一个简单的中心点来处理在一个请求周期中处理多个数据库的情况。
Sure its possible. The implementation would really depend on more specific requirements than what you have given but i would probably make some sort of
Db_Manager
class. that abstracts away all the details to keep the actions short and seet... then you might simply have an action like:This way you can handle all the actual sorting of which db to use and propagate to your models within the manager class. It would also give you an easy central point to handle working with multiple DB's in one request cycle if that comes up.