首頁 > php教程 > PHP开发 > 主體

ZendFramework框架實作連接兩個或多個資料庫的方法

黄舟
發布: 2016-12-14 10:34:41
原創
1309 人瀏覽過

本文實例講述了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(&#39;set NAMES utf8&#39;);
//$db->getProfiler()->setEnabled(false); 
Zend_Db_Table::setDefaultAdapter($db);
登入後複製

   

連接其他資料庫

$db = &$this->getAdapter();
登入後複製
登入後複製

呼叫 

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();
登入後複製
登入後複製

就可以了

來看一下完整的dao2.php

$this->db
登入後複製

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."
"; if ($pagesize) { $select->limit($pagesize, $offset); } //echo $select."
"; if (is_array($order)) { foreach ($order as $value) { $select->order($value); } } else { $select->order($order); } //echo $select."
"; $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."
"; return $result; } } else { if ($count) { $result = $this->db->fetchOne($select); //echo $select."
"; 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."
"; //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_; } }
登入後複製

.

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!