Zendframework连接两个或多个数据库的实现
2013-11-27 17:20
239 查看
配置文件
<db> <adapter>PDO_MSSQL</adapter> <config> <host>localhost</host> <port>1433</port> <username>sa</username> <password>123456</password> <dbname>edudb</dbname> <pdoType>sqlsrv</pdoType> </config> </db> <!-- 测试多数据库 --> <db2> <adapter>PDO_MSSQL</adapter> <config> <host>localhost</host> <port>1433</port> <username>sa</username> <password>123456</password> <dbname>test</dbname> <pdoType>sqlsrv</pdoType> </config> </db2>入口文件
//配置数据库连接 $db_config = $web_config->db->config->toArray(); //var_dump($db_config); $db = Zend_Db::factory($web_config->db->adapter, $db_config); //var_dump($db); //exit; //$db->query('set NAMES utf8'); //$db->getProfiler()->setEnabled(false); Zend_Db_Table::setDefaultAdapter($db);这里是默认的数据库dao.php调用默认数据库
$db = &$this->getAdapter();dao2.php连接其他数据库
function init() { $web_config = $this->getCfg(); $this->db2_config = $web_config->db2->config->toArray(); //var_dump($this->db_config); $this->db = Zend_Db::factory($web_config->db2->adapter, $this->db2_config); Zend_Db_Table::setDefaultAdapter($this->db); } public function returnDb(){ return $this->db; }调用
$db = &$this->getAdapter();直接使用
还是会连接默认数据库。
$this->db就可以了 来看一下完整的dao2.php
<?phpclass dao_dao2 extends Zend_Db_Table {
protected $cfg_;function init() { $web_config = $this->getCfg(); $this->db2_config = $web_config->db2->config->toArray(); //var_dump($this->db_config); $this->db = Zend_Db::factory($web_config->db2->adapter, $this->db2_config); Zend_Db_Table::setDefaultAdapter($this->db); } public function returnDb(){ return $this->db; }public function getData($table,$where = false, $order = 'id ASC', $pagesize = false, $offset = false, $count = false, $from = false, $join = false, $group = false) {
//$this->db = &$this->getAdapter();
$select = $this->db->select();if ($where && is_array($where)) {
foreach ($where as $key => $val) {
//print_r($where);
if($val['type']==1){
$select->where($key, $this->convert2gbk($val['val']));
}else{
$select->orwhere($key, $this->convert2gbk($val['val']));
}}
}if (!$from)
$from = '*';
//echo $select."<br/>";
if ($pagesize) {
$select->limit($pagesize, $offset);
}
//echo $select."<br/>";
if (is_array($order)) {
foreach ($order as $value) {
$select->order($value);
}
} else {
$select->order($order);
}
//echo $select."<br/>";
$select->from($table, $count ? "COUNT(".$table.".id)" : $from);if (is_array($group)) {
foreach ($group as $key => $val) {
$select->group($val);
}if ($count) {
$result = $this->db->fetchAll($select);
//echo $select."<br/>";
return $result;
}
} else {
if ($count) {
$result = $this->db->fetchOne($select);
//echo $select."<br/>";
return $result;
}
}
if (is_array($join)) {
foreach ($join as $key => $val) {
//$select->join($key, $val[0], $val[1]);
$select->joinleft($key, $val[0], $val[1]);
}
}//echo $select."<br/>";//echo $select;exit;$result = $this->db->fetchAll($select);
foreach ($result as $key => $value) {
foreach ($value as $key2 => $value2) {
$result[$key][$key2] = $this->convert2utf8($value2);
}
}
return $result;
}/**
* 向表中插入数据
* array $adata 数据
* string $table 表名
* int $insterid 是否需要返回插入ID
* @return true or false or int
*/
// @bianding 2013.11.04 更改了pdo中mssql.php的lastInsertId()函数
// @bianding 2013.11.04 经测试 mssql.php中的lastInsertId()函数中的SELECT两种方式都行
function SaveData($adata, $table, $insterid = 0, $aLog = false) {
//$this->db = &$this->getAdapter();
foreach ($adata as $key => $value) {
$adata[$key] = $this->convert2gbk($value);
}
if ($this->db->insert($table, $adata)) {
//var_dump($this->db->getProfiler());
$insertedID = $this->db->lastInsertId();
if ($insterid) {
return $insertedID;
} else {
return TRUE;
}
} else {
return false;
}
}/**
* 删除表中数据
*
* @param string $table 表名
* @param string $where 'id ='.$id 条件
* @return true or false
*/
function DelData($table, $where, $aLog = false) {
//$this->db = & $this->getAdapter();
if ($this->db->delete($table, $where)) {
return TRUE;
} else {
return FALSE;
}
}/**
* 更新表中数据
*
* @param string $table
* @param array $adata
* @param string $where 'id ='.$id
* @return true or false
*/
function UpdateData($table, $adata, $cond, $aLog = false) {
//$this->db = & $this->getAdapter();
foreach ($adata as $key => $value) {
$adata[$key] = $this->convert2gbk($value);
}
if ($this->db->update($table, $adata, $cond)) {
return TRUE;
} else {
return false;
}
}public function clearTable($table) {
//$this->db = &$this->getAdapter();
$result = $this->db->query('TRUNCATE TABLE ' . $table);
}public function executeSql($strSql) {
//$this->db = &$this->getAdapter();
$result = $this->db->query($strSql);
}function convert2utf8($string)
{
$config = $this->getCfg();
$pdoType = $config->db->config->pdoType;
if($pdoType == 'dblib'){
return iconv("gbk","utf-8",$string);
}elseif($pdoType == 'sqlsrv'){
//$encode = mb_detect_encoding($string, array('UTF-8',"GB2312",'GBK','BIG5'));
//echo $encode;
return mb_convert_encoding($string,"UTF-8","UTF-8");
//return $string;
}
}
function convert2gbk($string)
{
$config = $this->getCfg();
$pdoType = $config->db->config->pdoType;
if($pdoType == 'dblib'){
return iconv("utf-8","gbk",$string);
}elseif($pdoType == 'sqlsrv'){
//$encode = mb_detect_encoding($string, array('UTF-8',"GB2312",'GBK','BIG5'));
//echo $encode;
return mb_convert_encoding($string,"UTF-8","UTF-8");
//return $string;
}
}protected function &getCfg() {
if ($this->cfg_ === null) {
$registry = Zend_Registry::getInstance();
$this->cfg_ = $registry->get('web_config');
}
return $this->cfg_;
}}
相关文章推荐
- ZendFramework框架实现连接两个或多个数据库的方法
- ZendFramework框架实现连接两个或多个数据库的方法
- awk 如何处理两个文件,实现类似数据库表连接的操作
- Spring MVC配置双数据源实现一个java项目同时连接两个数据库的方法
- awk 如何处理两个文件,实现类似数据库表连接的操作
- qtp9.2 本地数据驱动参数化实现订票数据库等各个检查点(用两个Action实现)
- 利用DB Link实现Oracle两个数据库间的表同步
- Java实现JDBC连接数据库实例
- spring+hibernate下连接两个以上的数据库
- js连接AWS DynamoDB数据库实现CEUD操作
- Druid实现数据库连接用户密码加密
- java连接数据库方式(二):单独使用MyBatis 框架实现
- Java使用JDBC连接数据库的实现方法
- php实现mysql两个数据库中表的数据同步
- JDBC_使用DriverManager实现数据库的连接
- jsp实现连接数据库、查询、插入、修改功能的javaBean代码
- 用python连接数据库,实现用户注册和用户登录
- Python实现连接postgresql数据库的方法分析
- jsp连接数据库以及前端网页实现对数据库的增删改查
- JDBC 连接数据库(增,删,改,查)公用类及DAO类实现