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

时间:2009-05-28 14:06:01

标签: zend-framework zend-db

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

我已经实现了一个丑陋的修复,我创建了一个Database Table对象并在其上调用getAdapter()方法并通过它。但是,这是一种不好的方式,我希望它可以通过注册表获得。

有谁知道怎么做?任何有关正确方向的帮助或指示都会受到赞赏!

干杯 斯图尔特

聚苯乙烯。我正在使用Zend Framework 1.8。

9 个答案:

答案 0 :(得分:71)

如果您正在使用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);

答案 1 :(得分:12)

感谢您的回复。我决定改变接受的答案并发布我最终使用的解决方案 - 这最终非常简单!!

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

在bootstrap类中..

protected function _initDb()
{
    $resource = $bootstrap->getPluginResource('db');

    $db = $resource->getDbAdapter();

    Zend_Registry::set("db", $db);
}

然后使用...

访问其他地方
$dbAdapter = Zend_Registry::get("db");

感谢您的帮助,希望这有助于其他人。

答案 2 :(得分:7)

你错过了最好的事情:)

如果您使用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);
    }
}

答案 3 :(得分:4)

我的2美分......

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

来自Bootstrap:

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

从Controller有两种方法:

<?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);
?>

答案 4 :(得分:1)

检查zend-documentation: 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);

答案 5 :(得分:1)

我的引导程序中有一个方法可以将适配器添加到注册表中。我更喜欢更清洁的解决方案,但它确实有效:

protected function _initRegistry(){

    $this->bootstrap('db');
    $db = $this->getResource('db');

    $db->setFetchMode(Zend_Db::FETCH_OBJ);

    Zend_Registry::set('db', $db);
}

答案 6 :(得分:1)

这就是我的所作所为:

在引导程序中:

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 = 

看看:

答案 7 :(得分:1)

如果您使用的是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目录中找到直接与数据库表“交互”的模型部分将如下所示:

<?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 Framework生成的相同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"

这就是我没有更改引导程序文件的方法。

答案 8 :(得分:1)

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

因此,在您的控制器类中,您可以获得已在ini文件中定义的db适配器,如下所示:

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