【问题标题】:Zend > use database adapter with module ini configurationZend > 使用带有模块 ini 配置的数据库适配器
【发布时间】:2011-04-09 08:37:39
【问题描述】:

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

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

  • 申请
    • 模块
      • 管理员
        • 控制器
        • 型号
          • 数据库表
            • Users.php
        • 观看次数
        • 配置
          • module.ini
    • 配置
      • 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”模块的模型中使用这个配置。

仅供参考,我正在“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 或在管理模块的引导文件中设置任何选项?

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    您应该使用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 name.
        *
        * @var string
        */
       protected $_use_adapter = 'employees1';
    

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

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

    【讨论】:

    • @Zyzva:我对你回答的最后一部分感到困惑。我使用 zf 工具创建了 admin dbtable 模型,它的定义开始于:class Admin_Models_DbTable_Users extends Zend_Db_Table_Abstract
    • 你必须修复它,因为 zend 工具不依赖于 multidb 资源插件。创建一些 abstract class My_Class extends Zend_Db_Table_Abstract {//<put constructor which I wrote above here>} 。并从 My_Class 扩展您的模型:class Admin_Models_DbTable_Users extends My_Class {//...}
    【解决方案2】:

    根据我的一位朋友的建议,我正在采用不同的方法:

    在模块的引导文件中,我将模块的数据库适配器设置到注册表中,在模块的模型文件中,我使用自定义方法从注册表中获取模块的数据库适配器: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']; 
        }
    

    所以现在 admin 模块的 fetchUsers() 方法中的 $this->getAdapter() 将拥有“admin”模块的 db 适配器。 :)

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-14
        • 1970-01-01
        • 2011-01-27
        相关资源
        最近更新 更多