ZendFramework框架实现连接两个或多个数据库的方法
本文实例讲述了ZendFramework框架实现连接两个或多个数据库的方法。分享给大家供大家参考,具体如下:
配置文件:
<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('setNAMESutf8'); //$db->getProfiler()->setEnabled(false); Zend_Db_Table::setDefaultAdapter($db);
这里是默认的数据库
dao.php调用默认数据库
$db=&$this->getAdapter();
dao2.php连接其他数据库
functioninit(){ $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); } publicfunctionreturnDb(){ return$this->db; }
调用
$db=&$this->getAdapter();
还是会连接默认数据库。
直接使用
$this->db
就可以了
来看一下完整的dao2.php
<?php classdao_dao2extendsZend_Db_Table{ protected$cfg_; functioninit(){ $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); } publicfunctionreturnDb(){ return$this->db; } publicfunctiongetData($table,$where=false,$order='idASC',$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($whereas$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($orderas$value){ $select->order($value); } }else{ $select->order($order); } //echo$select."<br/>"; $select->from($table,$count?"COUNT(".$table.".id)":$from); if(is_array($group)){ foreach($groupas$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($joinas$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($resultas$key=>$value){ foreach($valueas$key2=>$value2){ $result[$key][$key2]=$this->convert2utf8($value2); } } return$result; } /** *向表中插入数据 *array$adata数据 *string$table表名 *int$insterid是否需要返回插入ID *@returntrueorfalseorint */ //@bianding2013.11.04更改了pdo中mssql.php的lastInsertId()函数 //@bianding2013.11.04经测试mssql.php中的lastInsertId()函数中的SELECT两种方式都行 functionSaveData($adata,$table,$insterid=0,$aLog=false){ //$this->db=&$this->getAdapter(); foreach($adataas$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{ returnTRUE; } }else{ returnfalse; } } /** *删除表中数据 * *@paramstring$table表名 *@paramstring$where'id='.$id条件 *@returntrueorfalse */ functionDelData($table,$where,$aLog=false){ //$this->db=&$this->getAdapter(); if($this->db->delete($table,$where)){ returnTRUE; }else{ returnFALSE; } } /** *更新表中数据 * *@paramstring$table *@paramarray$adata *@paramstring$where'id='.$id *@returntrueorfalse */ functionUpdateData($table,$adata,$cond,$aLog=false){ //$this->db=&$this->getAdapter(); foreach($adataas$key=>$value){ $adata[$key]=$this->convert2gbk($value); } if($this->db->update($table,$adata,$cond)){ returnTRUE; }else{ returnfalse; } } publicfunctionclearTable($table){ //$this->db=&$this->getAdapter(); $result=$this->db->query('TRUNCATETABLE'.$table); } publicfunctionexecuteSql($strSql){ //$this->db=&$this->getAdapter(); $result=$this->db->query($strSql); } functionconvert2utf8($string) { $config=$this->getCfg(); $pdoType=$config->db->config->pdoType; if($pdoType=='dblib'){ returniconv("gbk","utf-8",$string); }elseif($pdoType=='sqlsrv'){ //$encode=mb_detect_encoding($string,array('UTF-8',"GB2312",'GBK','BIG5')); //echo$encode; returnmb_convert_encoding($string,"UTF-8","UTF-8"); //return$string; } } functionconvert2gbk($string) { $config=$this->getCfg(); $pdoType=$config->db->config->pdoType; if($pdoType=='dblib'){ returniconv("utf-8","gbk",$string); }elseif($pdoType=='sqlsrv'){ //$encode=mb_detect_encoding($string,array('UTF-8',"GB2312",'GBK','BIG5')); //echo$encode; returnmb_convert_encoding($string,"UTF-8","UTF-8"); //return$string; } } protectedfunction&getCfg(){ if($this->cfg_===null){ $registry=Zend_Registry::getInstance(); $this->cfg_=$registry->get('web_config'); } return$this->cfg_; } }
更多关于zend相关内容感兴趣的读者可查看本站专题:《ZendFrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于ZendFramework框架的PHP程序设计有所帮助。