在 Registry 中注册 Zend 数据库适配器 [英] Registering Zend Database Adapter in Registry

查看:33
本文介绍了在 Registry 中注册 Zend 数据库适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在引导期间在注册表中注册对主数据库适配器的引用,以便它可以在我网站的其他地方使用(特别是授权操作).

I am looking to register a reference to the main Database Adapter in the Registry during Bootstrapping so it can be used elsewhere in my site (specifically the Authorisation action).

我已经实现了一个丑陋的修复程序,我创建了一个数据库表对象并在其上调用 getAdapter() 方法并通过它.但是,这是一种糟糕的做法,我希望它可以通过注册表使用.

I have implemented an ugly fix where i create a Database Table object and call the getAdapter() method on it and pass through that. However, this is a bad way of doing it and I would like it to be available via the registry.

有人知道怎么做吗?感谢您提供正确方向的任何帮助或指示!

Does anyone know how to do this? Any help or pointers in the right direction are appreciated!

干杯斯图尔特

附言.我使用的是 Zend Framework 1.8.

Ps. Im using Zend Framework 1.8.

推荐答案

如果您使用 Zend Framework 1.8+,并使用命令行工具创建您的项目,那么它就像在您的 application.ini 配置文件.

If you're using Zend Framework 1.8+, and created your project with the command line tool, then it's as simple as registering your database settings in your application.ini config file.

resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "your.database.host"
resources.db.params.dbname = "database_name"
resources.db.params.username = "username"
resources.db.params.password = "password"
resources.db.isDefaultTableAdapter = true

如果您的数据库设置以 resources.db 开头,您甚至不需要在 Bootstrap.php 文件中做任何事情,因为它会为您完成.此外,通过将 isDefaultTableAdapter 设置为 true,您可以在应用程序的任何位置获取数据库适配器的实例.

If your database settings are preceded by resources.db you won't even need to do anything in your Bootstrap.php file because it will do it for you. Also, by setting the isDefaultTableAdapter setting to true, you can get an instance of your database adapter anywhere in your application.

$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);

这篇关于在 Registry 中注册 Zend 数据库适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆