您好,登錄后才能下訂單哦!
使用ZendFramework框架怎么連接多個(gè)數(shù)據(jù)庫(kù)?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
配置文件:
<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> <!-- 測(cè)試多數(shù)據(jù)庫(kù) --> <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>
入口文件
//配置數(shù)據(jù)庫(kù)連接 $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);
這里是默認(rèn)的數(shù)據(jù)庫(kù)
dao.php調(diào)用默認(rèn)數(shù)據(jù)庫(kù)
$db = &$this->getAdapter();
dao2.php連接其他數(shù)據(jù)庫(kù)
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; }
調(diào)用
$db = &$this->getAdapter();
還是會(huì)連接默認(rèn)數(shù)據(jù)庫(kù)。
直接使用
$this->db
就可以了
來(lái)看一下完整的dao2.php
<?php class 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; } /** * 向表中插入數(shù)據(jù) * array $adata 數(shù)據(jù) * string $table 表名 * int $insterid 是否需要返回插入ID * @return true or false or int */ // @bianding 2013.11.04 更改了pdo中mssql.php的lastInsertId()函數(shù) // @bianding 2013.11.04 經(jīng)測(cè)試 mssql.php中的lastInsertId()函數(shù)中的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; } } /** * 刪除表中數(shù)據(jù) * * @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; } } /** * 更新表中數(shù)據(jù) * * @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_; } }
關(guān)于使用ZendFramework框架怎么連接多個(gè)數(shù)據(jù)庫(kù)問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。