zend框架实现支持sql server的操作方法

 更新时间:2016年12月08日 10:45:08   作者:牛逼的霍啸林  
这篇文章主要介绍了zend框架实现支持sql server的操作方法,结合实例形式分析了zend框架的相关代码修改、配置文件设置与相关问题注意事项,需要的朋友可以参考下

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

相关文章

  • ThinkPHP框架基于PDO方式连接数据库操作示例

    ThinkPHP框架基于PDO方式连接数据库操作示例

    这篇文章主要介绍了ThinkPHP框架基于PDO方式连接数据库操作,结合完整实例形式分析了thinkPHP使用PDO方式连接数据库的相关配置、控制器及模板调用相关操作技巧,需要的朋友可以参考下
    2018-03-03
  • php面向对象中的魔术方法中文说明

    php面向对象中的魔术方法中文说明

    这篇文章主要介绍了php面向对象中的魔术方法中文说明,明白这些方法才好写面向对象程序,需要的朋友可以参考下
    2014-03-03
  • PHP动态生成javascript文件的2个例子

    PHP动态生成javascript文件的2个例子

    这篇文章主要介绍了PHP动态生成javascript文件的2个例子,需要的朋友可以参考下
    2014-04-04
  • PHP实现微信提现功能(微信商城)

    PHP实现微信提现功能(微信商城)

    这篇文章主要介绍了PHP实现微信提现功能,此类功能在微信商城中经常会用到,今天小编通过实例代码给大家讲解,需要的朋友可以参考下
    2019-11-11
  • php微信公众号开发之答题连闯三关

    php微信公众号开发之答题连闯三关

    这篇文章主要为大家详细介绍了php微信公众号开发之答题连闯三关,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • phpstorm安装xdebug(phpstudy环境下)成功运行的操作步骤

    phpstorm安装xdebug(phpstudy环境下)成功运行的操作步骤

    这篇文章主要介绍了phpstorm安装xdebug(phpstudy环境下)成功运行,本文分步骤给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • php微信公众平台开发(三)订阅事件处理

    php微信公众平台开发(三)订阅事件处理

    这篇文章主要介绍了php微信公众平台开发中的订阅事件处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Laravel框架控制器,视图及模型操作图文详解

    Laravel框架控制器,视图及模型操作图文详解

    这篇文章主要介绍了Laravel框架控制器,视图及模型操作,结合实例形式详细分析了laravel框架控制器,视图及模型的基本功能、原理与相关操作使用技巧,需要的朋友可以参考下
    2019-12-12
  • PHP 实现数组分页

    PHP 实现数组分页

    在日常开发的业务环境中,我们一般都会使用MySQL语句来实现分页的功能。但是,往往也有些数据并不多,或者只是获取 PHP 中定义的一些数组数据时需要分页的功能。这时,我们可以在一次查询中把所有的数据取出来,然后在 PHP 的代码层面进行分页功能的实现
    2021-06-06
  • php木马webshell扫描器代码

    php木马webshell扫描器代码

    因为前端时间服务器被放过 所以写了个webshell扫描器 呵呵 专杀php webshell 不管大马还是小马 包括一句话 现在放出代码来
    2012-01-01

最新评论