【问题标题】:Module-specific database config in Zend FrameworkZend 框架中特定于模块的数据库配置
【发布时间】:2013-03-02 17:49:07
【问题描述】:

我希望我的 Zend Framework 模块之一使用与其他模块不同的数据库。 The Manual suggests 您可以在 application.ini 中的资源前加上模块名称来实现这一点,但我无法让它工作。

application.ini的相关位是:

资源.modules[] = "" resources.db.adapter = "PDO_MYSQL" resources.db.params.host = "本地主机" 资源.db.params.dbname = "maindb" resources.db.params.username = "dbuser" 资源.db.params.password = "..." resources.db.params.charset = "utf8" terms.resources.db.params.dbname = "termsdb"

其中terms 是模块的名称。

我需要在 Bootstrap 中添加什么特别的东西来完成这项工作吗?

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    编辑:

    关于 OP 在下面的第一条评论...

    不,我无法详细说明如何设置模块化数据库资源。我相信我提供的理论上应该有效。如果我不确定在Zend_Application_Resource_Db 的扩展中需要修改什么,因为我没有使用该资源。我有自己的自定义资源,它允许多个数据库并根据唯一的连接名称获取这些数据库连接。但是我可以详细说明:-)

    所以我有一个扩展 Zend_DbMyLib_Db 类,它看起来像这样:

    class MyLib_Db extends Zend_Db
    {
       protected $_instance = null;
       protected $_connections = array();
    
       /**
        * Standard Zend Framework unified constructor
        * @param null|array An array of options that will be passed to setOptions
        */
       public function __construct($options = null)
       {
       }
    
       /**
        * Standard Zend Framework setOptions implementation
        * @param array $options and array of options from config
        * @return MyLib_Db
        */
       public function setOptions(array $options)
       {
       }
    
       /**
        * Set the class instance
        * @param MyLib_Db
        * @return MyLib_Db
        */
       public static function setInstance($instance)
       {
       }
    
       /**
        * Get/create the singleton instance
        * @return MyLib_Db
        */
       public static function getInstance()
       {
       }
    
       /**
        * Get a Zend_Db adapter Instance by unique name
        * Searches self::$_connections for the $name param as an array key
        * @param String $name unique connection name
        * @return Zend_Db_Adpater_Abstract|null
        */
       public function getConnection($connectionName)
       {
       }
    
       /**
        * Add a connection instance to the registry
        * Adds/creates an Zend_Db_Adapter instance to the connection registry with
        * the string key provided by $name. If $connection is an array|Zend_Config it 
        * should match the format used by Zend_Db::factory as it will be passed to this   
        * function. If $name is null then the database name will be used. 
        * @param Zend_Db_Adapter_Abstract|Zend_Config|array The connection to register
        * @param string|null $name A unique name for the connection
        * @return MyLib_Db
        */
       public function addConnection(Zend_Db_Adapter_Abstract $connection, $name = null)
       {
       }
    
       /**
        * Remove/Destroy the specified connection from the registry
        * @param string $name the connection name to remove
        * @return MyLib_Db
        */
       public function removeConnection($name)
       {
       }
    }
    

    所以基本上我的 DB 应用程序资源创建并返回前一个类的实例。在创建过程中,它会创建我在配置中设置的所有适配器,并在此类实例中使用名称注册它们(它还会查找与 Zend_Db_Table 一起使用的默认标志以及做一些其他事情)。

    然后我要么使用MyLib_Db::getInstance()->getConnection($name); 或者我从引导程序中获取MyLib_Db 实例,然后调用getConnection

    我个人更喜欢这样做,因为它不依赖于应用范围内的连接或绑定到允许更灵活重用的特定模块。也就是说,我实际上只在几个项目中使用过它,因为在我的大部分 Zend 项目中,我一直在使用 Doctrine 而不是 Zend_Db

    希望有所帮助:-)


    实际上,我认为您需要将其放在配置中的模块部分中,例如 modules.terms.resources.db.*。这应该使其加载到模块引导程序中。或者,您可以在您的Terms_Bootstrap 中使用_initDb 手动设置它。

    虽然我个人使用了一个特殊的数据库管理类并将其放入我的资源中 - 它处理设置单个或多个适配器。然后在下面假设$db 是从资源数组中检索到的经理...

    $dbAdapter = $db->getConnection('terms');

    【讨论】:

    • 第一个建议对我不起作用。您能否详细说明如何在 Bootstrap 中进行设置?
    【解决方案2】:

    如下操作:

    moduleName.host = "localhost"
    moduleName.username = "user"
    moduleName.password = "pass"
    moduleName.dbname = "dbname"
    moduleName.charset = "utf8"
    

    然后在你的模块的 Bootstrap.php 中的任何初始化函数中添加以下内容

    $params = $GLOBALS['application']->getOption('moduleName');
    $dbAdapter = Zend_Db::factory('PDO_MYSQL', array(
        'host' => $params['host'],
        'dbname' => $params['dbname'],
        'username' => $params['username'],
        'password' => $params['password'],
        'charset'  => $params['charset'],
    ));
    Zend_Registry::set('moduleSpecific', $dbAdapter);
    

    然后在您模块的model/DbTable/ 文件夹中创建一个类,如下所示,让您的表类扩展 ModuleName_Model_DbTable_Tablemain 而不是 Zend_Db_Table_Abstract,然后您就可以开始了。

    class ModuleName_Model_DbTable_Tablemain extends Zend_Db_Table_Abstract
    {
        /**
         * 
         * wrapper class constructor used to set db adaptor on the fly
         * @author Krishan
         */
        function __construct(){
    
            parent::__construct($adapter = Zend_Registry::get('moduleSpecific'));
    
        }
    }
    

    如果有任何问题请告诉我

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      相关资源
      最近更新 更多