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 相关文章推荐
基于文本的留言簿
Oct 09 PHP
PHP clearstatcache()函数详解
Mar 02 PHP
我的php学习笔记(毕业设计)
Feb 21 PHP
深入理解PHP中的global
Aug 19 PHP
php中fsockopen用法实例
Jan 05 PHP
PHP实现自动对图片进行滚动显示的方法
Mar 12 PHP
使用JavaScript创建新样式表和新样式规则
Jun 14 PHP
PDO的安全处理与事物处理方法
Oct 31 PHP
Yii2框架制作RESTful风格的API快速入门教程
Nov 08 PHP
基于ThinkPHP实现的日历功能实例详解
Apr 15 PHP
PHP编程获取图片的主色调的方法【基于Imagick扩展】
Aug 02 PHP
laravel框架添加数据,显示数据,返回成功值的方法
Oct 11 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开发框架的对比
2013/07/05 PHP
解决php接收shell返回的结果中文乱码问题
2014/01/23 PHP
ThinkPHP3.2.1图片验证码实现方法
2016/08/19 PHP
JavaScript isPrototypeOf和hasOwnProperty使用区别
2010/03/04 Javascript
javascript forEach通用循环遍历方法
2010/10/11 Javascript
jquery实现对联广告的方法
2015/02/05 Javascript
JS实现的4种数字千位符格式化方法分享
2015/03/02 Javascript
js实现支持手机滑动切换的轮播图片效果实例
2015/04/29 Javascript
js省市联动效果完整实例代码
2015/12/09 Javascript
Node.js配合node-http-proxy解决本地开发ajax跨域问题
2016/08/31 Javascript
谈谈target=_new和_blank的不同之处
2016/10/25 Javascript
JavaScript中常见的八个陷阱总结
2017/06/28 Javascript
AngularJS select设置默认值的实现方法
2017/08/25 Javascript
vue引入jq插件的实例讲解
2017/09/12 Javascript
webpack中CommonsChunkPlugin详细教程(小结)
2017/11/09 Javascript
JS实现调用本地摄像头功能示例
2018/05/18 Javascript
jQuery实现table表格checkbox全选的方法分析
2018/07/04 jQuery
微信小程序时间控件picker view使用详解
2018/12/28 Javascript
Node.js中Koa2在控制台输出请求日志的方法示例
2019/05/02 Javascript
vue 实现input表单元素的disabled示例
2019/10/28 Javascript
JavaScript实现简单的图片切换功能(实例代码)
2020/04/10 Javascript
Python 正则表达式实现计算器功能
2017/04/29 Python
深入理解Python中的*重复运算符
2017/10/28 Python
django使用LDAP验证的方法示例
2018/12/10 Python
Jupyter notebook在mac:linux上的配置和远程访问的方法
2019/01/14 Python
Django框架之DRF 基于mixins来封装的视图详解
2019/07/23 Python
Django 实现前端图片压缩功能的方法
2019/08/07 Python
pytorch实现保证每次运行使用的随机数都相同
2020/02/20 Python
python 等差数列末项计算方式
2020/05/03 Python
美国杂志订阅折扣与优惠网站:Magazines.com
2016/08/31 全球购物
马智宇结婚主持词
2014/04/01 职场文书
就业协议书盖章的注意事项
2014/09/28 职场文书
教师读书笔记
2015/06/29 职场文书
2016初一新生军训心得体会
2016/01/11 职场文书
Nginx进程管理和重载原理详解
2021/04/22 Servers
PostgreSQL之连接失败的问题及解决
2023/05/08 PostgreSQL