37

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

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

有谁知道如何做到这一点?任何帮助或正确方向的指示都将不胜感激!

干杯斯图尔特

附言。我使用 Zend Framework 1.8。

4

9 回答 9

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);
于 2009-11-24T00:09:56.500 回答
12

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

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

在引导类中..

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

    $db = $resource->getDbAdapter();

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

然后在其他地方访问...

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

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

于 2009-05-29T13:05:17.817 回答
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);
    }
}
于 2009-05-29T13:21:54.710 回答
4

我的2美分...

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

从引导程序:

<?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);
?>
于 2010-08-27T16:05:57.103 回答
1

检查 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);
于 2009-05-28T14:11:57.883 回答
1

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

protected function _initRegistry(){

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

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

    Zend_Registry::set('db', $db);
}
于 2009-05-28T15:48:13.603 回答
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 = 

看一眼:

于 2009-05-28T15:59:04.300 回答
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"

这就是我在无需更改引导文件的情况下所做的。

于 2009-06-09T06:44:40.080 回答
1

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

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

$bootstrap = $this->getInvokeArg('bootstrap');
$resource = $bootstrap->getPluginResource('db');
$db = $resource->getDbAdapter();
于 2009-09-24T17:54:26.093 回答