代码之家  ›  专栏  ›  技术社区  ›  Stuart

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

  •  37
  • Stuart  · 技术社区  · 17 年前

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

    我已经实现了一个丑陋的修复,我创建了一个数据库表对象,并对其调用getAdapters()方法并传递该方法。然而,这是一种糟糕的方式,我希望它能通过注册表提供。

    我使用的是Zend Framework 1.8。

    9 回复  |  直到 4 年前
        1
  •  71
  •   Andrew    16 年前

    如果您使用的是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);
    
        2
  •  12
  •   zzlalani M I    12 年前

    谢谢你的回复。我决定改变公认的答案,并发布我最终使用的解决方案——这最终非常简单!!

    这基本上是基于 D阿姨 评论。..

    在bootstrap类中。。

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

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

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

    谢谢你的帮助,希望这能帮助别人。

        3
  •  7
  •   Ian Warner    17 年前

    你错过了最好的东西:)

    如果你使用Zend_Db_Table模型(你应该使用)等,那么你可以设置一个默认适配器——这样当你实例化一个模型时,它所处理的Db连接——这样你就不需要在运行模型查询之前将其保存在注册表中,也不必担心连接问题。

    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);
        }
    }
    
        4
  •  4
  •   Julian    16 年前

    我的2美分。..

    如何获取默认的DB适配器:

    来自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);
    ?>
    
        5
  •  1
  •   Silfverstrom    17 年前

    查看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);
    
        6
  •  1
  •   David Snabel-Caunt    17 年前

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

    protected function _initRegistry(){
    
        $this->bootstrap('db');
        $db = $this->getResource('db');
    
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
    
        Zend_Registry::set('db', $db);
    }
    
        7
  •  1
  •   Boris Guéry    17 年前

    以下是我的工作:

    引导程序内部:

    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 = 
    

    看看:

        8
  •  1
  •   wenbert    17 年前

    如果你使用的是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"
    

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

        9
  •  1
  •   Vishal Parpia    16 年前

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

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

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