繁体   English   中英

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

[英]Module-specific database config in Zend Framework

我希望我的Zend Framework模块之一使用与其他模块不同的数据库。 手册建议您可以使用模块名称为application.ini的资源添加前缀,以实现此目的,但我无法使其正常工作。

我的application.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是模块的名称。

为了使这项工作有效,我需要在Bootstrap中放入一些特殊的东西吗?

进行如下操作:

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

然后在任何init函数中在模块的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'));

    }
}

如有任何问题请通知我

编辑:

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

不,我无法详细说明如何设置模块化数据库资源。 我提供的资料在理论上应该有效。 如果不是,我不确定在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)
   {
   }
}

因此,基本上,我的数据库应用程序资源创建并返回了先前类的实例。 在创建过程中,它将创建我在配置中设置的所有适配器,并使用一个名称将其注册到该类实例中(它还会查找用于Zend_Db_Table的默认标志以及进行其他操作)。

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

我个人更喜欢这样做,因为它不依赖于应用程序范围内的连接,也不依赖于特定模块的连接,从而可以更灵活地重用。 那就是说我实际上只在几个项目中使用了它,因为在我的大多数Zend项目中,我一直在使用Doctrine而不是Zend_Db

希望有帮助:-)


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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM