Php Zend>;将数据库适配器与模块ini配置一起使用
我使用的是带有Zend框架的模块化体系结构,我希望使用特定于模块的数据库配置,以便每个模块的模型都使用自己的数据库配置 这是我的示例应用程序体系结构:Php Zend>;将数据库适配器与模块ini配置一起使用,php,zend-framework,Php,Zend Framework,我使用的是带有Zend框架的模块化体系结构,我希望使用特定于模块的数据库配置,以便每个模块的模型都使用自己的数据库配置 这是我的示例应用程序体系结构: 应用 模块 管理员 控制器 模型 数据库表 Users.php 观点 配置 模块ini 配置 application.ini 从上面的体系结构中,我有模块“admin”,我在admin/configs/module.ini中指定了它的数据库配置,如: resources.db.adapter = "pdo_mysq
- 应用
- 模块
- 管理员
- 控制器
- 模型
- 数据库表
- Users.php
- 数据库表
- 观点
- 配置
- 模块ini
- 管理员
- 配置
- application.ini
- 模块
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "*****"
resources.db.params.password = "*****"
resources.db.params.dbname = "********"
现在我想在“管理”模块的模型中使用此配置
仅供参考,Iam在“管理”模块的控制器中实例化模型,如:
$admin = new Admin_Models_DbTable_Users();
$admin_users = $admin->fetchUsers();
public function fetchUsers()
{
$sql = "select * from users";
return $this->getAdapter()->fetchAll($sql, array(), Zend_Db::FETCH_OBJ);
}
在“管理”模块的模型中,我执行如下查询:
$admin = new Admin_Models_DbTable_Users();
$admin_users = $admin->fetchUsers();
public function fetchUsers()
{
$sql = "select * from users";
return $this->getAdapter()->fetchAll($sql, array(), Zend_Db::FETCH_OBJ);
}
如何在“管理”模块的数据库适配器中加载admin/configs/module.ini中的数据库配置,使其使用该配置?我是否需要使用Zend注册表或在管理模块的引导文件中设置任何选项 您应该使用。简而言之,在application.ini中,您应该设置所有数据库:
resources.multidb.employees.adapter = PDO_MYSQL
resources.multidb.employees.host = localhost
resources.multidb.employees.username = zend
resources.multidb.employees.password = zend
resources.multidb.employees.dbname = zend
resources.multidb.employees.default = true
resources.multidb.employees1.adapter = "PDO_SQLITE"
resources.multidb.employees1.dbname = APPLICATION_PATH "/../data/db/employeesdb.db"
然后在Bootstrap.php中,您应该将它们保存到注册表中:
public function run()
{
$resource = $this->getPluginResource('multidb');
Zend_Registry::set('news', $resource->getDb('employees1'));
Zend_Registry::set('employees', $resource->getDb('employees'));
parent::run();
}
最后,在db模型类中,您应该编写:
/**
* Db name.
*
* @var string
*/
protected $_use_adapter = 'employees1';
和(这部分可以放在某个抽象类中,该类扩展了Zend_Db_Table_abstract,并将成为所有Db模型类的父类):
你应该使用。简而言之,在application.ini中,您应该设置所有数据库:
resources.multidb.employees.adapter = PDO_MYSQL
resources.multidb.employees.host = localhost
resources.multidb.employees.username = zend
resources.multidb.employees.password = zend
resources.multidb.employees.dbname = zend
resources.multidb.employees.default = true
resources.multidb.employees1.adapter = "PDO_SQLITE"
resources.multidb.employees1.dbname = APPLICATION_PATH "/../data/db/employeesdb.db"
然后在Bootstrap.php中,您应该将它们保存到注册表中:
public function run()
{
$resource = $this->getPluginResource('multidb');
Zend_Registry::set('news', $resource->getDb('employees1'));
Zend_Registry::set('employees', $resource->getDb('employees'));
parent::run();
}
最后,在db模型类中,您应该编写:
/**
* Db name.
*
* @var string
*/
protected $_use_adapter = 'employees1';
和(这部分可以放在某个抽象类中,该类扩展了Zend_Db_Table_abstract,并将成为所有Db模型类的父类):
仅供参考:我的朋友也想这么做。他在application.ini中设置了一个默认适配器。然后,在每个zend_db模型中,他希望使用另一个数据库,他更改了模式配置。他说这是最简单的。然后他的数据库模型已经有了身份验证和主机,他只是重写了数据库和表。一定要考虑这个选择;很抱歉,我没有任何链接或示例。仅供参考:我的朋友也想这样做。他在application.ini中设置了一个默认适配器。然后,在每个zend_db模型中,他希望使用另一个数据库,他更改了模式配置。他说这是最简单的。然后他的数据库模型已经有了身份验证和主机,他只是重写了数据库和表。一定要考虑这个选择;很抱歉,我没有任何链接或示例。根据我的一位朋友的建议,我采用了不同的方法: 在模块的引导文件中,我将模块的db适配器设置到注册表中,在模块的模型文件中,我使用自定义方法从注册表中获取模块的db适配器:getAdapter() 在…application/modules/admin/Bootstrap.php中编写代码
protected function _initAutoload()
{
$configuration = new Zend_Config_Ini(
APPLICATION_PATH . '/modules/admin/configs/module.ini',
'production'
);
$params = $configuration->resources->db->params->toArray();
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
Zend_Registry::set('DB',$DB);
}
public function getAdapter()
{
$registry = Zend_Registry::getInstance();
return $registry['DB'];
}
在…application/modules/admin/DbTable/Users.php中编写代码
protected function _initAutoload()
{
$configuration = new Zend_Config_Ini(
APPLICATION_PATH . '/modules/admin/configs/module.ini',
'production'
);
$params = $configuration->resources->db->params->toArray();
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
Zend_Registry::set('DB',$DB);
}
public function getAdapter()
{
$registry = Zend_Registry::getInstance();
return $registry['DB'];
}
因此,现在管理模块的fetchUsers()方法中的$this->getAdapter()将具有“admin”模块的db适配器。:) 正如我的一位朋友所建议的那样,我采用了一种不同的方法: 在模块的引导文件中,我将模块的db适配器设置到注册表中,在模块的模型文件中,我使用自定义方法从注册表中获取模块的db适配器:getAdapter() 在…application/modules/admin/Bootstrap.php中编写代码
protected function _initAutoload()
{
$configuration = new Zend_Config_Ini(
APPLICATION_PATH . '/modules/admin/configs/module.ini',
'production'
);
$params = $configuration->resources->db->params->toArray();
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
Zend_Registry::set('DB',$DB);
}
public function getAdapter()
{
$registry = Zend_Registry::getInstance();
return $registry['DB'];
}
在…application/modules/admin/DbTable/Users.php中编写代码
protected function _initAutoload()
{
$configuration = new Zend_Config_Ini(
APPLICATION_PATH . '/modules/admin/configs/module.ini',
'production'
);
$params = $configuration->resources->db->params->toArray();
$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
Zend_Registry::set('DB',$DB);
}
public function getAdapter()
{
$registry = Zend_Registry::getInstance();
return $registry['DB'];
}
因此,现在管理模块的fetchUsers()方法中的$this->getAdapter()将具有“admin”模块的db适配器。:) @Zyzva:我对你最后的回答感到困惑。我使用zf工具创建了admin dbtable模型,它的定义如下:类admin_Models_dbtable_Users extensed Zend_Db_Table_AbstractYou必须修复它,因为Zend工具不依赖于multidb资源插件。创建一些
抽象类我的类扩展了Zend\u Db\u Table\u abstract{/}
。并从My_类扩展您的模型:Class Admin_models_DbTable_用户扩展My_类{/…}
@Zyzva:我对您答案的最后一部分感到困惑。我使用zf工具创建了admin dbtable模型,它的定义如下:类admin_Models_dbtable_Users extensed Zend_Db_Table_AbstractYou必须修复它,因为Zend工具不依赖于multidb资源插件。创建一些抽象类我的类扩展了Zend\u Db\u Table\u abstract{/}
。并从My_类扩展您的模型:Class Admin_models_DbTable_用户扩展My_类{/…}