Zend>使用带有模块ini配置的数据库适配器

时间:2011-04-09 08:37:39

标签: php zend-framework

我正在使用Zend框架的模块化架构,我想使用特定于模块的数据库配置,以便每个模块的模型都使用自己的数据库配置。

这是我的示例应用程序架构:

  • 应用
    • 模块
      • 管理员
        • 控制器
        • 模型
          • DBTABLE
            • Users.php
        • 视图
        • CONFIGS
          • module.ini中
    • CONFIGS
      • 的application.ini

从上面的架构中,我有“ admin ”模块,我在 admin / configs / module.ini 中指定了数据库配置,如:

resources.db.adapter = "pdo_mysql" 
resources.db.params.host = "localhost" 
resources.db.params.username = "*****" 
resources.db.params.password = "*****" 
resources.db.params.dbname = "********"

现在我想在“admin”模块的模型中使用此配置。

仅供参考,Iam在“admin”模块的控制器中实例化模型,如:

$admin = new Admin_Models_DbTable_Users();
$admin_users = $admin->fetchUsers();

在“admin”模块的模型中,我正在执行以下查询:

public function fetchUsers()
{
$sql = "select * from users";
return $this->getAdapter()->fetchAll($sql, array(), Zend_Db::FETCH_OBJ);
}

如何在我的“admin”模块的数据库适配器中的 admin / configs / module.ini 中加载数据库配置,以便它使用该配置?我是否需要使用Zend Registry或在admin模块的bootstrap文件中设置任何选项?

3 个答案:

答案 0 :(得分:3)

您应该使用multidb resource plugin。简而言之,在application.ini中,您应该设置所有数据库:

resources.multidb.employees.adapter = PDO_MYSQL
resources.multidb.employees.host = localhost
resources.multidb.employees.username = zend
resources.multidb.employees.password = zend
resources.multidb.employees.dbname = zend
resources.multidb.employees.default = true

resources.multidb.employees1.adapter = "PDO_SQLITE"
resources.multidb.employees1.dbname = APPLICATION_PATH "/../data/db/employeesdb.db"

然后在你的Bootstrap.php中你应该将它们保存到注册表中:

public function run()
{
    $resource = $this->getPluginResource('multidb');
    Zend_Registry::set('news', $resource->getDb('employees1'));
    Zend_Registry::set('employees', $resource->getDb('employees'));

    parent::run();
}

最后在您的db模型类中,您应该写:

  /**
    * Db name.
    *
    * @var string
    */
   protected $_use_adapter = 'employees1';

和(这部分可以放到一些扩展Zend_Db_Table_Abstract的抽象类中,并且将成为所有db模型类的父类):

   public function __construct($config = null)
   {
       if(isset($this->_use_adapter)){
           $config = $this->_use_adapter;
       }
       return parent::__construct($config);
   }

答案 1 :(得分:2)

我正在按照我的一位朋友的建议采用不同的方法:

在模块的bootstrap文件中,我将模块的db适配器设置到Registry中,在模块的模型文件中,我使用自定义方法从注册表获取模块的db适配器:getAdapter()

代码.... application / modules / admin / Bootstrap.php

protected function _initAutoload()
    {       
        $configuration = new Zend_Config_Ini(
                APPLICATION_PATH . '/modules/admin/configs/module.ini', 
                'production'
                );

        $params = $configuration->resources->db->params->toArray();
        $DB = new Zend_Db_Adapter_Pdo_Mysql($params);

        Zend_Registry::set('DB',$DB);       

    }

代码在... application / modules / admin / DbTable / Users.php

public function getAdapter()
    {   
        $registry   = Zend_Registry::getInstance();     
        return $registry['DB']; 
    }

现在,管理模块的 fetchUsers()方法中的 $ this-> getAdapter()将具有“admin”模块的数据库适配器。 :)

答案 2 :(得分:1)

仅供参考:我的朋友也想这样做。他在application.ini中设置了默认适配器。然后在他想要使用另一个数据库的每个zend_db模型中,他更改了模式配置。他说这是最简单的。然后他的db模型已经有了身份验证和主机,他只是覆盖了数据库和表。绝对考虑这个选项;抱歉,我没有任何链接或示例。