欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ZendFramework框架实现连接两个或多个数据库的方法

程序员文章站 2024-04-01 20:48:28
本文实例讲述了zendframework框架实现连接两个或多个数据库的方法。分享给大家供大家参考,具体如下: 配置文件: &l...

本文实例讲述了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('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

<?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;
  }
  /**
   * 向表中插入数据
   * 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_;
  } 
}

更多关于zend相关内容感兴趣的读者可查看本站专题:《zend framework框架入门教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于zend framework框架的php程序设计有所帮助。