[英]Module-specific database config in Zend Framework
I want one of my Zend Framework modules to use a different database from the others. 我希望我的Zend Framework模块之一使用与其他模块不同的数据库。 The Manual suggests you can prefix the resources in
application.ini
with the module name to achieve this but I can't get it to work. 手册建议您可以使用模块名称为
application.ini
的资源添加前缀,以实现此目的,但我无法使其正常工作。
The relevant bits of my application.ini
are: 我的
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"
where terms
is the name of the module. 其中
terms
是模块的名称。
Is there something special I need to put in the Bootstrap to make this work? 为了使这项工作有效,我需要在Bootstrap中放入一些特殊的东西吗?
Do as follows: 进行如下操作:
moduleName.host = "localhost"
moduleName.username = "user"
moduleName.password = "pass"
moduleName.dbname = "dbname"
moduleName.charset = "utf8"
Then add following in your Module's Bootstrap.php in any init function 然后在任何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);
Then in your module's model/DbTable/
folder create a class as follows and let your table classess to extend ModuleName_Model_DbTable_Tablemain instead of Zend_Db_Table_Abstract and you are ready to go. 然后在模块的
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'));
}
}
let me know in case of any problem 如有任何问题请通知我
EDIT: 编辑:
In regards to the OP's first comment below... 关于OP在下面的第一条评论...
No I cant elaborate on setting up a modular DB resource. 不,我无法详细说明如何设置模块化数据库资源。 What i provided should work in theory i believe.
我提供的资料在理论上应该有效。 If it doesnt im not sure what needs to be modified in an extension of
Zend_Application_Resource_Db
because as i eluded to i dont use that resource. 如果不是,我不确定在
Zend_Application_Resource_Db
的扩展名中需要修改什么,因为我逃避使用该资源。 I have my own custom resource that allows for multiple dbs and fetching those db connections based on unique connection names. 我有自己的自定义资源,该资源允许多个数据库并根据唯一的连接名称获取这些数据库连接。 I can however ellaborate on that :-)
但是我可以在这方面炫耀:-)
So i have a MyLib_Db
class that extends Zend_Db
it looks something like this: 所以我有一个
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)
{
}
}
So basically my application resource for DB creates and returns an instance of the preceeding class. 因此,基本上,我的数据库应用程序资源创建并返回了先前类的实例。 During creation it creates any adapters i have set up in my configuration and registers them in this class instance with a name (it also looks for a default flag for use with
Zend_Db_Table
as well as doing some other stuff). 在创建过程中,它将创建我在配置中设置的所有适配器,并使用一个名称将其注册到该类实例中(它还会查找用于
Zend_Db_Table
的默认标志以及进行其他操作)。
Then i either use MyLib_Db::getInstance()->getConnection($name);
然后我要么使用
MyLib_Db::getInstance()->getConnection($name);
or i get the MyLib_Db
instance from the bootstrap and then call getConnection
. 或者我从引导程序中获取
MyLib_Db
实例,然后调用getConnection
。
Personally i prefer to do it this way because its not dependent on having a connection being app wide or tied to a specific module which allows for more flexible reuse. 我个人更喜欢这样做,因为它不依赖于应用程序范围内的连接,也不依赖于特定模块的连接,从而可以更灵活地重用。 That said i actually have only used this on a couple projects because on most of my Zend projects Ive been using Doctrine instead of
Zend_Db
. 那就是说我实际上只在几个项目中使用了它,因为在我的大多数Zend项目中,我一直在使用Doctrine而不是
Zend_Db
。
Hope that helps :-) 希望有帮助:-)
Actually i think you need to put that in your Module's section in the config like modules.terms.resources.db.*
. 实际上,我认为您需要将其放置在配置的
modules.terms.resources.db.*
部分中,例如modules.terms.resources.db.*
。 This should make it load in the module bootstrap. 这应该使其加载到模块引导程序中。 Alternatively you could manullay set it up with an
_initDb
in your Terms_Bootstrap
. 或者,您可以在自己的
Terms_Bootstrap
使用_initDb
进行Terms_Bootstrap
。
Personally though i use a special DB management class and put that in my resources instead - it handles setting a single or multiple adapters. 就我个人而言,尽管我使用特殊的数据库管理类并将其放在我的资源中-它处理设置单个或多个适配器。 Then assiming in the following that
$db
is the manager retrieved from the resources array... 然后在下面假设
$db
是从资源数组中检索到的管理器...
$dbAdapter = $db->getConnection('terms');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.