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

Zend框架中特定于模块的数据库配置

  •  1
  • Tamlyn  · 技术社区  · 15 年前

    我希望我的一个Zend框架模块使用与其他模块不同的数据库。 The Manual suggests 您可以在 application.ini 用模块名来实现这一点,但我不能让它工作。

    我的相关资料 应用程序.ini 是:

    resources.modules[] = ""
    
    resources.db.adapter = "PDO_MYSQL"
    resources.db.params.host = "localhost"
    resources.db.params.dbname = "maindb"
    resources.db.params.username = "dbuser"
    resources.db.params.password = "..."
    resources.db.params.charset = "utf8"
    
    terms.resources.db.params.dbname = "termsdb"
    

    哪里 terms 是模块的名称。

    有什么特别的东西我需要放在鞋带,使这项工作?

    2 回复  |  直到 15 年前
        1
  •  0
  •   prodigitalson    15 年前

    编辑:

    关于下面的评论。。。

    不,我不能详细说明如何设置模块化数据库资源。我所提供的理论上应该有用。如果它不确定需要在的扩展中修改什么 Zend_Application_Resource_Db 因为当我逃避的时候,我不使用那个资源。我有自己的自定义资源,允许多个数据库,并基于唯一的连接名获取这些数据库连接。不过,我可以用它来证明:-)

    所以我有一个 MyLib_Db 扩展的类 Zend_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应用程序资源创建并返回一个preceeding类的实例。在创建过程中,它会创建我在配置中设置的任何适配器,并用一个名称将它们注册到这个类实例中(它还会寻找一个默认标志,用于 Zend_Db_Table 以及做一些其他的事情)。

    那我要么用 MyLib_Db::getInstance()->getConnection($name); 或者我得到 迈利布数据库 实例,然后调用 getConnection .

    就个人而言,我更喜欢这样做,因为它不依赖于连接是应用程序宽或绑定到一个特定的模块,允许更灵活的重用。也就是说,我实际上只在几个项目中使用过这个,因为在我的Zend项目中,我大部分都在使用教义,而不是 Zend_数据库 .

    希望有帮助:-)


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

    就我个人而言,尽管我使用了一个特殊的数据库管理类,并将其放在我的资源中——它处理设置单个或多个适配器。然后在下面协助 $db 是从资源数组中检索的管理器。。。

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

        2
  •  0
  •   Krishan Gopal    12 年前

    执行以下操作:

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

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

    $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_Db table_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'));
    
        }
    }
    

    如果有什么问题请告诉我