编辑:
关于 OP 在下面的第一条评论...
不,我无法详细说明如何设置模块化数据库资源。我相信我提供的理论上应该有效。如果我不确定在Zend_Application_Resource_Db 的扩展中需要修改什么,因为我没有使用该资源。我有自己的自定义资源,它允许多个数据库并根据唯一的连接名称获取这些数据库连接。但是我可以详细说明:-)
所以我有一个扩展 Zend_Db 的 MyLib_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');