在registry中注册Zend数据库适配器

我期待在引导过程中在registry中注册对主数据库适配器的引用,以便可以在我的站点的其他地方使用(特别是授权操作)。

我已经实现了一个丑陋的修复,我创build一个数据库表对象,并调用它的getAdapter()方法,并通过它。 但是,这是一个不好的方法,我希望它可以通过registry。

有谁知道如何做到这一点? 任何帮助或指针在正确的方向表示赞赏!

干杯斯图尔特

PS。 我使用Zend Framework 1.8。

如果您使用Zend Framework 1.8+,并使用命令行工具创build项目,那么就像在application.iniconfiguration文件中注册数据库设置一样简单。

 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 ,您可以在应用程序的任何位置获取数据库适配器的实例。

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

感谢您的答复。 我决定改变接受的答案,并张贴我最终使用的解决scheme – 这是非常简单的!

这基本上是基于Dcaunt的评论…

在bootstrap类中..

 protected function _initDb() { $resource = $bootstrap->getPluginResource('db'); $db = $resource->getDbAdapter(); Zend_Registry::set("db", $db); } 

然后访问其他地方…

 $dbAdapter = Zend_Registry::get("db"); 

感谢您的帮助,并希望这可以帮助别人。

你错过了最好的东西:)

如果你使用Zend_Db_Table模型(你应该是)等等,那么你可以设置一个默认的适配器 – 这样当你实例化一个模型的数据库连接它照顾 – 这样你真的不需要将其保存在registry中或打扰关于通过模型运行查询之前的连接。

如果需要的话,我会将它保存在registry中供以后使用 – 但是我可以删除它

 protected function _initDB() { // Check that the config contains the correct database array. if ($this->_config->db) { // Instantiate the DB factory $dbAdapter = Zend_Db::factory($this->_config->db); // Set the DB Table default adaptor for auto connection in the models Zend_Db_Table::setDefaultAdapter($dbAdapter); // Add the DB Adaptor to the registry if we need to call it outside of the modules. Zend_Registry::set('dbAdapter', $dbAdapter); } } 

我的2分钱…

如何获取默认的数据库适配器:

从Bootstrap:

 <?php $dbResource = $this->getPluginResource('db'); db = $dbResource->getDbAdapter(); var_dump($db); ?> 

从一个控制器有两种方法:

 <?php // Method 1 $bootstrap = $this->getInvokeArg('bootstrap'); $dbResource = $bootstrap->getPluginResource('db'); $dbAdapter = $dbResource->getDbAdapter(); var_dump($dbAdapter); // Method 2 $dbAdapter = Zend_Db_Table::getDefaultAdapter(); var_dump($dbAdapter); ?> 

检查zend-documentation:15.5.3.3。 将数据库适配器存储在registry中

http://framework.zend.com/manual/en/zend.db.table.html

 $db = Zend_Db::factory('PDO_MYSQL', $options); Zend_Registry::set('my_db', $db); // Later... $table = new Bugs(array('db' => 'my_db')); 

像你正在寻找的东西?

编辑:
从一个ini文件加载你的configuration,使用:
parse_ini_file($ ini文件)

 ;configuration.ini host = 127.0.0.1 user = username password = blabla ;yourfile.php $options = parse_ini_file('configuration.ini'); $db = Zend_Db::factory('PDO_MYSQL', $options); 

我有一个方法在我的引导添加到registry的适配器。 我更喜欢更清洁的解决scheme,但它的工作原理:

 protected function _initRegistry(){ $this->bootstrap('db'); $db = $this->getResource('db'); $db->setFetchMode(Zend_Db::FETCH_OBJ); Zend_Registry::set('db', $db); } 

这是我做的:

在bootstrap里面:

 define('CONFIG_FILE', '../config/general.ini'); define('APP_MODE', 'development'); 

在初始化程序中:

  /** * Initialize data bases * * @return void */ public function initDb () { $options = Zend_Registry::get('conf'); $db = Zend_Db::factory($options->database); $db->query(new Zend_Db_Expr('SET NAMES utf8')); Zend_Registry::set('db', $db); } public function initConfig () { if (file_exists(CONFIG_FILE) && is_readable(CONFIG_FILE)) { $conf = new Zend_Config_Ini(CONFIG_FILE, APP_MODE); Zend_Registry::set('conf', $conf); } else { throw new Zend_Config_Exception('Unable to load config file'); } } 

最后,我的configuration文件如下所示:

 [production] database.adapter = pdo_Mysql database.params.host = db.example.com database.params.username = dbuser database.params.password = secret database.params.dbname = dbname ; Overloaded configuration from production [development : production] database.params.host = localhost database.params.username = root database.params.password = 

看一眼:

  • Zend_Db的::厂()
  • Zend_Config_Ini的
  • 合适的词汇

如果您正在使用Zend Framework 1.8,只需在控制器/操作中执行以下操作:

 class CreateorderController extends Zend_Controller_Action { public function testAction() { //more code $users_obj = new Default_Model_Users(); //this would load the model using the Default namespace //more code } } 

我的Defaul_Model_Users类看起来像这样:

 <?php /** * application/models/Users.php */ class Default_Model_Users extends Zend_Db_Table { protected $_table; public function getTable() { if(null === $this->_table) { $this->_table = new Default_Model_DbTable_Users(); } return $this->_table; } public function fetchAll() { $result = $this->getTable()->fetchAll(); return $result; } } 

在DbTable目录中find与数据库表“直接交互”的模型的部分将如下所示:

 <?php /** * application/models/DbTable/Users.php */ class Default_Model_DbTable_Users extends Zend_Db_Table_Abstract { /** Table name */ protected $_name = 'users'; public function init() { $this->_db->setFetchMode(Zend_Db::FETCH_OBJ); } } 

然后,我会有这样一个小的增加Zend框架生成相同的application.ini:

 resources.db.adapter = "PDO_MYSQL" resources.db.params.host = "localhost" resources.db.params.dbname = "mydb" resources.db.params.username = "root" resources.db.params.password = "password" 

这就是我没有改变引导文件而没有做的事情。

我不想使用registry来存储我应该能够访问的对象,所以我做了一点挖掘。 事实certificate,引导程序被注册为前端控制器参数“bootstrap”,可以从您的任何控制器访问,如Zend_Application的本手册页中所述 。

所以在你的控制器类中,你可以得到在你的ini文件中定义的数据库适配器,如下所示:

 $bootstrap = $this->getInvokeArg('bootstrap'); $resource = $bootstrap->getPluginResource('db'); $db = $resource->getDbAdapter();