我希望在引导过程中向注册表中注册主数据库适配器的引用,以便其他地方可以使用它(特别是授权操作)。
我已经实现了一个丑陋的解决方法,其中我创建了一个数据库表对象并调用其getAdapter()方法并通过它进行传递。但是,这是一种不好的做法,我希望它可以通过注册表提供。
有谁知道怎么做吗?任何帮助或指向正确方向的提示都将不胜感激!
我正在使用Zend Framework 1.8。
我希望在引导过程中向注册表中注册主数据库适配器的引用,以便其他地方可以使用它(特别是授权操作)。
我已经实现了一个丑陋的解决方法,其中我创建了一个数据库表对象并调用其getAdapter()方法并通过它进行传递。但是,这是一种不好的做法,我希望它可以通过注册表提供。
有谁知道怎么做吗?任何帮助或指向正确方向的提示都将不胜感激!
我正在使用Zend Framework 1.8。
application.ini
配置文件中注册您的数据库设置即可。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);
$dbAdapter
.. getDefaultAdapter()
应该是在整个文档中始终引用当前$dbAdapter
的默认方式。天啊,SO从未让我失望过。 - Mahdi.Montgomeryprotected function _initDb()
{
$resource = $bootstrap->getPluginResource('db');
$db = $resource->getDbAdapter();
Zend_Registry::set("db", $db);
}
然后在其他地方访问它,使用...
$dbAdapter = Zend_Registry::get("db");
感谢帮助,希望能对其他人有所帮助。
你错过了最好的东西 :)
如果你使用Zend_Db_Table模型(你应该这样做)等,那么你可以设置一个默认的适配器 - 这样当你实例化一个模型时,数据库连接就被处理了 - 这样你不需要真正保存它在注册表中或在通过模型运行查询之前打扰连接。
如果需要,我会将其保存在注册表中以备后用,但我可能会删除此功能。
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);
}
}
我个人的看法...
如何获取默认的数据库适配器:
从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);
?>
我不想使用注册表来存储我应该能够访问的对象,所以我进行了一些调查。结果发现引导程序被注册为前端控制器参数“bootstrap”,可以从任何控制器中访问,如Zend_Application的手册页面中所解释的那样。
因此,在您的控制器类中,您可以像这样获取已在ini文件中定义的db适配器:
$bootstrap = $this->getInvokeArg('bootstrap');
$resource = $bootstrap->getPluginResource('db');
$db = $resource->getDbAdapter();
请查看Zend文档: 15.5.3.3. 在注册表中存储数据库适配器
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 文件中加载配置,请使用:
parse_ini_file($inifile)
;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);
我在我的引导程序中有一个方法来将适配器添加到注册表中。虽然我更喜欢一个更简洁的解决方案,但它确实有效:
protected function _initRegistry(){
$this->bootstrap('db');
$db = $this->getResource('db');
$db->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::set('db', $db);
}
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');
}
}
最终我的配置文件看起来像这样:
[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 =
请看一下:
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目录中,其代码如下:
<?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"
这就是我做到的,而不必改变bootstrap文件。