zend框架实现支持sql server的操作方法


Posted in PHP onDecember 08, 2016

本文实例讲述了zend框架实现支持sql server的操作方法。分享给大家供大家参考,具体如下:

1.修改Zend/Db/Adapter/Pdo/Abstract.php中的connect方法

protected function _connect()
{
  // if we already have a PDO object, no need to re-connect.
  if ($this->_connection) {
    return;
  }
  // get the dsn first, because some adapters alter the $_pdoType
  $dsn = $this->_dsn();
  // check for PDO extension
  if (!extension_loaded('pdo')) {
    /**
     * [url=home.php?mod=space&uid=86763]@see[/url] Zend_Db_Adapter_Exception
     */
    require_once 'Zend/Db/Adapter/Exception.php';
    throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
  }
  // check the PDO driver is available
  if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) {
    /**
     * @see Zend_Db_Adapter_Exception
     */
    require_once 'Zend/Db/Adapter/Exception.php';
    throw new Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed');
  }
  // create PDO connection
  $q = $this->_profiler->queryStart('connect', Zend_Db_Profiler::CONNECT);
  // add the persistence flag if we find it in our config array
  if (isset($this->_config['persistent']) && ($this->_config['persistent'] == true)) {
    $this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true;
  }
  try {
    //print_r($this->_config);exit;
    if($this->_config['pdoType']=='sqlsrv'){
      $this->_connection = new PDO( "sqlsrv:Server=".$this->_config['host'].";Database = ".$this->_config['dbname'], $this->_config['username'], $this->_config['password']);
      $this->_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
      $this->_connection->setAttribute( PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8 );
      $this->_profiler->queryEnd($q);
    }elseif ($this->_config['pdoType']=='dblib') {
      $this->_connection = new PDO(
        $dsn,
        $this->_config['username'],
        $this->_config['password'],
        $this->_config['driver_options']
      );
      $this->_profiler->queryEnd($q);
    }
    // set the PDO connection to perform case-folding on array keys, or not
    $this->_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding);
    // always use exceptions.
    $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  } catch (PDOException $e) {
    /**
     * @see Zend_Db_Adapter_Exception
     */
    require_once 'Zend/Db/Adapter/Exception.php';
    throw new Zend_Db_Adapter_Exception($e->getMessage());
  }
}

这里针对linux和windows提供两种连接方式。

2.mssql.php 中的为 protected $_pdoType = 'sqlsrv';

protected function _dsn()
{
    // baseline of DSN parts
    $dsn = $this->_config;
    // don't pass the username and password in the DSN
    unset($dsn['username']);
    unset($dsn['password']);
    unset($dsn['driver_options']);
    if (isset($dsn['port'])) {
      $seperator = ':';
      if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        $seperator = ',';
      }
      $dsn['host'] .= $seperator . $dsn['port'];
      unset($dsn['port']);
    }
    // this driver supports multiple DSN prefixes
    // @see http://www.php.net/manual/en/ref.pdo-dblib.connection.php
    //print_r($dsn);exit;
    if (isset($dsn['pdoType'])) {
      switch (strtolower($dsn['pdoType'])) {
        case 'freetds':
        case 'sybase':
          $this->_pdoType = 'sybase';
          break;
        case 'mssql':
          $this->_pdoType = 'mssql';
          break;
        case 'sqlsrv':
          $this->_pdoType = 'sqlsrv';
          break;
        case 'dblib':
        default:
          $this->_pdoType = 'dblib';
          break;
      }
      unset($dsn['pdoType']);
    }
    // use all remaining parts in the DSN
    foreach ($dsn as $key => $val) {
      $dsn[$key] = "$key=$val";
    }
    $dsn = $this->_pdoType . ':' . implode(';', $dsn);
   // print_r($dsn);exit;
    return $dsn;
}

3.ZF 的web.xml 数据库配置文件改成:

<db>
  <adapter>PDO_MSSQL</adapter>
<config>
    <host>localhost</host>
    <username>sa</username>
    <password>123456</password>
    <dbname>testdb </dbname>
    <pdoType>sqlsrv</pdoType>
  </config>
</db>

期间遇到中文乱码问题

function convert2utf8($string)
{
    $config = $this->getCfg();
    $pdoType = $config->db->config->pdoType;
    if($pdoType == 'dblib'){
      return iconv("gbk","utf-8",$string);
    }elseif($pdoType == 'sqlsrv'){
      return mb_convert_encoding($string,"UTF-8","auto");
    }
}
function convert2gbk($string)
{
    $config = $this->getCfg();
    $pdoType = $config->db->config->pdoType;
    if($pdoType == 'dblib'){
      return iconv("utf-8","gbk",$string);
    }elseif($pdoType == 'sqlsrv'){
      return mb_convert_encoding($string,"GBK","auto");
    }
}
protected function &getCfg() {
    if ($this->cfg_ === null) {
      $registry = Zend_Registry::getInstance();
      $this->cfg_ = $registry->get('web_config');
    }
    return $this->cfg_;
}

针对不同的类型,进行不同的处理。

希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

PHP 相关文章推荐
对javascript和select部件的结合运用
Oct 09 PHP
PHP新手上路(三)
Oct 09 PHP
基于Snoopy的PHP近似完美获取网站编码的代码
Oct 23 PHP
PHP 数组和字符串互相转换实现方法
Mar 26 PHP
php 常用算法和时间复杂度
Jul 01 PHP
Linux下创建nginx脚本-start、stop、reload…
Aug 03 PHP
php常用的安全过滤函数集锦
Oct 09 PHP
Thinkphp将二维数组变为标签适用的一维数组方法总结
Oct 30 PHP
thinkphp3.2.2实现生成多张缩略图的方法
Dec 19 PHP
PHP实现的限制IP投票程序IP来源分析
May 04 PHP
CI框架无限级分类+递归的实现代码
Nov 01 PHP
php抽象类和接口知识点整理总结
Aug 02 PHP
ZendFramework框架实现连接两个或多个数据库的方法
Dec 08 #PHP
thinkPHP模板引擎用法示例
Dec 08 #PHP
thinkPHP中session()方法用法详解
Dec 08 #PHP
thinkPHP引入类的方法详解
Dec 08 #PHP
PHP对象、模式与实践之高级特性分析
Dec 08 #PHP
php中__toString()方法用法示例
Dec 07 #PHP
php中this关键字用法分析
Dec 07 #PHP
You might like
从PHP的源码中深入了解stdClass类
2014/04/18 PHP
php分页函数完整实例代码
2014/09/22 PHP
php将12小时制转换成24小时制的方法
2015/03/31 PHP
Laravel 5 框架入门(一)
2015/04/09 PHP
PHP+Mysql+jQuery文件下载次数统计实例讲解
2015/10/10 PHP
PHP内置加密函数详解
2016/11/20 PHP
PHP中TP5 上传文件的实例详解
2017/07/31 PHP
Laravel框架Eloquent ORM修改数据操作示例
2019/12/03 PHP
锋利的jQuery 要点归纳(三) jQuery中的事件和动画(下:动画篇)
2010/03/24 Javascript
jqPlot 基于jquery的画图插件
2011/04/26 Javascript
js简单实现点击左右运动的方法
2015/04/10 Javascript
js实现TAB切换对应不同颜色的代码
2015/08/31 Javascript
javascript如何写热点图
2015/12/08 Javascript
使用json来定义函数,在里面可以定义多个函数的实现方法
2016/10/28 Javascript
Nodejs基于LRU算法实现的缓存处理操作示例
2017/03/17 NodeJs
js获取指定时间的前几秒
2017/04/05 Javascript
JS Math对象与Math方法实例小结
2019/07/05 Javascript
在Vue 中获取下拉框的文本及选项值操作
2020/08/13 Javascript
[40:31]Secret vs Alliacne 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
Tornado Web服务器多进程启动的2个方法
2014/08/04 Python
Python解析json文件相关知识学习
2016/03/01 Python
Python的网络编程库Gevent的安装及使用技巧
2016/06/24 Python
详解python使用Nginx和uWSGI来运行Python应用
2018/01/09 Python
基于Python数据结构之递归与回溯搜索
2020/02/26 Python
pycharm 多行批量缩进和反向缩进快捷键介绍
2021/01/15 Python
基于Python的接口自动化unittest测试框架和ddt数据驱动详解
2021/01/27 Python
请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值
2014/09/15 面试题
专科毕业生自我鉴定
2013/12/01 职场文书
机关门卫制度
2014/02/01 职场文书
《藏戏》教学反思
2014/02/11 职场文书
保险公司开门红口号
2014/06/21 职场文书
光学与应用专业毕业生求职信
2014/09/01 职场文书
科级干部群众路线教育实践活动个人对照检查材料
2014/09/19 职场文书
警察正风肃纪剖析材料
2014/10/16 职场文书
推广普通话主题班会
2015/08/17 职场文书
Matplotlib可视化之添加让统计图变得简单易懂的注释
2021/06/11 Python