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 相关文章推荐
Apache设置虚拟WEB
Oct 09 PHP
一个程序下载的管理程序(四)
Oct 09 PHP
Codeigniter+PHPExcel实现导出数据到Excel文件
Jun 12 PHP
Linux下创建nginx脚本-start、stop、reload…
Aug 03 PHP
PHP动态编译出现Cannot find autoconf的解决方法
Nov 05 PHP
十幅图告诉你什么是PHP引用
Feb 22 PHP
php获取json数据所有的节点路径
May 17 PHP
PHP使用逆波兰式计算工资的方法
Jul 29 PHP
php视频拍照上传头像功能实现代码分享
Oct 08 PHP
功能强大的PHP POST提交数据类
Jul 15 PHP
php获取微信共享收货地址的方法
Dec 21 PHP
PHP命名空间与自动加载机制的基础介绍
Aug 25 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
Discuz Uchome ajaxpost小技巧
2011/01/04 PHP
yii2.0实现创建简单widgets示例
2016/07/18 PHP
yii框架无限极分类的实现方法
2017/04/08 PHP
PHP使用redis消息队列发布微博的方法示例
2017/06/22 PHP
PHP各种常见经典算法总结【排序、查找、翻转等】
2019/08/05 PHP
laravel-admin解决表单select联动时,编辑默认没选上的问题
2019/09/30 PHP
表格 隔行换色升级版
2009/11/07 Javascript
从jQuery.camelCase()学习string.replace() 函数学习
2011/09/13 Javascript
JS数学函数Exp使用说明
2012/08/09 Javascript
js使用心得分享
2015/01/13 Javascript
纯js代码实现简单计算器
2015/12/02 Javascript
Angular中$compile源码分析
2016/01/28 Javascript
ECharts仪表盘实例代码(附源码下载)
2016/02/18 Javascript
一个炫酷的Bootstrap导航菜单
2016/12/28 Javascript
纯JS实现的读取excel文件内容功能示例【支持所有浏览器】
2018/06/23 Javascript
JS 中可以提升幸福度的小技巧(可以识别更多另类写法)
2018/07/28 Javascript
Vue项目自动转换 px 为 rem的实现方法
2018/10/29 Javascript
解决layui使用layui-icon出现默认图标的问题
2019/09/11 Javascript
[37:22]DOTA2上海特级锦标赛D组资格赛#2 Liquid VS VP第一局
2016/02/28 DOTA
Python中的面向对象编程详解(下)
2015/04/13 Python
详解Python中 __get__和__getattr__和__getattribute__的区别
2016/06/16 Python
Python 实现简单的shell sed替换功能(实例讲解)
2017/09/29 Python
python用列表生成式写嵌套循环的方法
2018/11/08 Python
python中的colorlog库使用详解
2019/07/05 Python
python处理大日志文件
2019/07/23 Python
Python如何对齐字符串
2020/07/30 Python
python实现自动清理重复文件
2020/08/24 Python
HTML5+CSS3绘制锯齿状的矩形
2016/03/01 HTML / CSS
求职意向书
2014/04/01 职场文书
2014年幼儿园工作总结
2014/11/10 职场文书
队名及霸气口号大全
2015/12/25 职场文书
如何写一份成功的商业计划书
2019/06/25 职场文书
解决pytorch读取自制数据集出现过的问题
2021/05/31 Python
使用RedisTemplat实现简单的分布式锁
2021/11/20 Redis
分析SQL窗口函数之排名窗口函数
2022/04/21 Oracle
Python使用Beautiful Soup(BS4)库解析HTML和XML
2022/06/05 Python