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 相关文章推荐
第五节 克隆 [5]
Oct 09 PHP
MySQL数据库转移,access,sql server 转 MySQL 的图文教程
Sep 02 PHP
php数组应用之比较两个时间的相减排序
Aug 18 PHP
php运行出现Call to undefined function curl_init()的解决方法
Nov 02 PHP
深入探讨PHP中的内存管理问题
Aug 31 PHP
Linux下实现PHP多进程的方法分享
Aug 16 PHP
PHP 透明水印生成代码
Aug 27 PHP
php去除换行符的方法小结(PHP_EOL变量的使用)
Feb 16 PHP
Codeigniter上传图片出现“You did not select a file to upload”错误解决办法
Jun 12 PHP
windows7下安装php的php-ssh2扩展教程
Jul 04 PHP
PHP中文乱码解决方案
Mar 05 PHP
php实现递归的三种基本方式
Jul 04 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字符串截取问题
2006/11/28 PHP
php session 预定义数组
2009/03/16 PHP
浅谈php扩展imagick
2014/06/02 PHP
2014最热门的24个php类库汇总
2014/12/18 PHP
7个鲜为人知却非常实用的PHP函数
2015/07/01 PHP
php高清晰度无损图片压缩功能的实现代码
2018/12/09 PHP
php实现的证件照换底色功能示例【人像抠图/换背景图】
2020/05/29 PHP
JavaScript 函数式编程的原理
2009/10/16 Javascript
如何将JS的变量值传递给ASP变量
2012/12/10 Javascript
jQuery解析Json实例详解
2015/11/24 Javascript
全面理解闭包机制
2016/07/11 Javascript
js 性能优化之算法和流程控制
2017/02/15 Javascript
JavaScript的for循环中嵌套一个点击事件的问题解决
2017/03/03 Javascript
jQuery日程管理控件glDatePicker用法详解
2017/03/29 jQuery
详解在AngularJS的controller外部直接获取$scope
2017/06/02 Javascript
js实现图片旋转 js滚动鼠标中间对图片放大缩小
2017/07/05 Javascript
10行原生JS实现文字无缝滚动(超简单)
2018/01/02 Javascript
layUI实现列表查询功能
2019/07/27 Javascript
浅谈webpack和webpack-cli模块源码分析
2020/01/19 Javascript
使用Vue+Django+Ant Design做一个留言评论模块的示例代码
2020/06/01 Javascript
js中实现继承的五种方法
2021/01/25 Javascript
Python简单实现子网掩码转换的方法
2016/04/13 Python
python thrift搭建服务端和客户端测试程序
2018/01/17 Python
浅析python参数的知识点
2018/12/10 Python
在python中,使用scatter绘制散点图的实例
2019/07/03 Python
Pytorch 的损失函数Loss function使用详解
2020/01/02 Python
python实现数字炸弹游戏
2020/07/17 Python
PyTorch安装与基本使用详解
2020/08/31 Python
Python字符串查找基本操作代码案例
2020/10/27 Python
Skip Hop官网:好莱坞宝宝挚爱品牌
2018/06/17 全球购物
adidas瑞典官方网站:购买阿迪达斯鞋子和运动服
2019/12/11 全球购物
群众路线教育实践活动方案
2014/02/02 职场文书
民族团结先进集体事迹材料
2014/05/22 职场文书
党性教育心得体会
2014/09/03 职场文书
vue-cli3.0修改打包后的文件名和文件地址,打包后本地运行报错解决
2022/04/06 Vue.js
使用Cargo工具高效创建Rust项目
2022/08/14 Javascript