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 相关文章推荐
傻瓜化配置PHP环境――Appserv
Dec 13 PHP
php批量缩放图片的代码[ini参数控制]
Feb 11 PHP
Window下PHP三种运行方式图文详解
Jun 11 PHP
php去除HTML标签实例
Nov 06 PHP
PHP类的反射用法实例
Nov 03 PHP
PHP调用Linux命令权限不足问题解决方法
Feb 07 PHP
CodeIgniter连贯操作的底层原理分析
May 17 PHP
使用PHP连接多种数据库的实现代码(mysql,access,sqlserver,Oracle)
Dec 21 PHP
laravel手动创建数组分页的实现代码
Jun 07 PHP
php实现数字补零的方法总结
Sep 12 PHP
PHP进阶学习之垃圾回收机制详解
Jun 18 PHP
PHP新手指南
Apr 01 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 破解防盗链图片函数
2008/12/09 PHP
PHP strip_tags()去除HTML、XML以及PHP的标签介绍
2014/02/18 PHP
CodeIgniter记录错误日志的方法全面总结
2016/05/17 PHP
学习ExtJS form布局
2009/10/08 Javascript
统计出现最多的字符次数的js代码
2010/12/03 Javascript
使用Raygun对Node.js应用进行错误处理的方法
2015/06/23 Javascript
jQuery网页选项卡插件rTabs用法实例分析
2015/08/26 Javascript
关于微信中a链接无法跳转问题
2016/08/02 Javascript
jQuery实现侧浮窗与中浮窗切换效果的方法
2016/09/05 Javascript
利用JS轻松实现获取表单数据
2016/12/06 Javascript
vue父子组件的嵌套的示例代码
2017/09/08 Javascript
Bootstrap开发中Tab标签页切换图表显示问题的解决方法
2018/07/13 Javascript
Vue 项目分环境打包的方法示例
2018/08/03 Javascript
vue router 用户登陆功能的实例代码
2019/04/24 Javascript
在Layui 的表格模板中,实现layer父页面和子页面传值交互的方法
2019/09/10 Javascript
create-react-app中添加less支持的实现
2019/11/15 Javascript
vue 实现LED数字时钟效果(开箱即用)
2019/12/08 Javascript
浅析vue-router实现原理及两种模式
2020/02/11 Javascript
python轻松实现代码编码格式转换
2015/03/26 Python
Python竟能画这么漂亮的花,帅呆了(代码分享)
2017/11/15 Python
django配置连接数据库及原生sql语句的使用方法
2019/03/03 Python
python或C++读取指定文件夹下的所有图片
2019/08/31 Python
Tensorflow全局设置可见GPU编号操作
2020/06/30 Python
python利用后缀表达式实现计算器功能
2021/02/22 Python
法国创作个性化T恤衫和其他定制产品平台:Tostadora
2018/04/08 全球购物
Boom手表官网:瑞典手表品牌,设计你的手表
2019/03/11 全球购物
Deux par Deux官方网站:设计师童装
2020/01/03 全球购物
香港百佳网上超级市场:PARKNSHOP.com
2020/06/10 全球购物
Java面试题:请说出如下代码的输出结果
2013/04/22 面试题
超市营业员求职简历的自我评价
2013/10/17 职场文书
乔迁宴答谢词
2014/01/21 职场文书
历史专业大学生职业生涯规划书
2014/03/13 职场文书
新品发布会策划方案
2014/06/08 职场文书
2014领导班子四风问题对照检查材料思想汇报
2014/09/21 职场文书
2014物价局群众路线对照检查材料思想汇报
2014/09/21 职场文书
新手初学Java List 接口
2021/07/07 Java/Android