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 相关文章推荐
vs中通过剪切板循环来循环粘贴不同内容
Apr 30 PHP
php curl选项列表(超详细)
Jul 01 PHP
ThinkPHP实现将本地文件打包成zip下载
Jun 26 PHP
php实现的支持imagemagick及gd库两种处理的缩略图生成类
Sep 23 PHP
php递归json类实例
Dec 02 PHP
PHP中JSON的应用技巧
Oct 10 PHP
深入浅析用PHP实现MVC
Mar 02 PHP
深入理解PHP类的自动载入机制
Sep 16 PHP
PHP中引用类型和值类型功能与用法示例
Feb 26 PHP
Mac下快速搭建PHP开发环境步骤详解
May 05 PHP
PHP面向对象类型约束用法分析
Jun 12 PHP
再谈Yii Framework框架中的事件event原理与应用
Apr 07 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写的MySQL数据库用户认证系统代码
2007/03/22 PHP
PHP SPL 被遗落的宝石【SPL应用浅析】
2018/04/20 PHP
thinkPHP+LayUI 流加载实现功能
2019/09/27 PHP
laravel-admin 实现在指定的相册下添加照片
2019/10/21 PHP
Laravel 微信小程序后端实现用户登录的示例代码
2019/11/26 PHP
通用JS事件写法实现代码
2009/01/07 Javascript
JavaScript读取中文cookie时的乱码问题的解决方法
2009/10/14 Javascript
js 省地市级联选择
2010/02/07 Javascript
onclick与listeners的执行先后问题详细解剖
2013/01/07 Javascript
Node.js实现JS文件合并小工具
2016/02/02 Javascript
jQuery实现控制文字内容溢出用省略号(…)表示的方法
2016/02/26 Javascript
node模块机制与异步处理详解
2016/03/13 Javascript
jquery获取所有选中的checkbox实现代码
2016/05/26 Javascript
浅谈JavaScript对象的创建方式
2016/06/13 Javascript
利用Vue.js实现checkbox的全选反选效果
2017/01/18 Javascript
jQuery实现选中行变色效果(实例讲解)
2017/07/06 jQuery
Vue.js@2.6.10更新内置错误处机制Fundebug同步支持相应错误监控
2019/05/13 Javascript
浅谈对于“不用setInterval,用setTimeout”的理解
2019/08/28 Javascript
[42:32]完美世界DOTA2联赛PWL S2 LBZS vs FTD.C 第二场 11.27
2020/12/01 DOTA
Python查找两个有序列表中位数的方法【基于归并算法】
2018/04/20 Python
Python中函数参数调用方式分析
2018/08/09 Python
详解pandas.DataFrame中删除包涵特定字符串所在的行
2019/04/04 Python
Python中Flask-RESTful编写API接口(小白入门)
2019/12/11 Python
Django Serializer HiddenField隐藏字段实例
2020/03/31 Python
基于pycharm实现批量修改变量名
2020/06/02 Python
Python基于traceback模块获取异常信息
2020/07/23 Python
windows+vscode安装paddleOCR运行环境的步骤
2020/11/11 Python
美国正宗奢华复古手袋、珠宝及配饰网站:What Goes Around Comes Around
2018/07/21 全球购物
C#基础面试题
2016/10/17 面试题
轻化专业学生实习自我鉴定
2013/09/20 职场文书
校园招聘策划书
2014/01/09 职场文书
项目经理聘任书
2014/03/29 职场文书
企业标语大全
2014/07/01 职场文书
领导干部作风建设剖析材料
2014/10/11 职场文书
2016大学生优秀志愿者事迹材料
2016/02/25 职场文书
Java服务调用RestTemplate与HttpClient的使用详解
2022/06/21 Java/Android