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

本文实例讲述了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程序设计有所帮助。

(0)

相关推荐

  • Zend Framework开发入门经典教程

    本文讲述了Zend Framework开发入门相关知识点.分享给大家供大家参考,具体如下: Zend Framework发布了!虽然仍处于开发初期,这个教程仍突出讲解目前几个最好的功能,并指导你完成一个简单程序的构建. Zend最早在社区里发布了ZF.基于同样的想法,这个教程写来用于展示ZF现有的功能.由于这个教程是在线发布,我将在ZF变化时对其进行更新,以便尽可能有效. 要求 Zend Framework要求PHP5.为了更好利用本教程的代码,你还需要Apache网页服务器.因为示范程序(一个

  • Zend Framework教程之Zend_Registry对象用法分析

    本文实例讲述了Zend Framework教程之Zend_Registry对象用法.分享给大家供大家参考,具体如下: 使用对象注册表(Registry) 对象注册表(或称对象仓库)是一个用于在整个应用空间(application space)内存储对象和值的容器.通过把对象存储在其中,我们可以在整个项目的任何地方使用同一个对象.这种机制相当于一种全局存储. 我们可以通过Zend_Registry类的静态方法来使用对象注册表,另外,由于该类是一个数组对象,你可以使用数组形式来访问其中的类方法. 1

  • ZendFramework框架实现连接两个或多个数据库的方法

    本文实例讲述了ZendFramework框架实现连接两个或多个数据库的方法.分享给大家供大家参考,具体如下: 配置文件: <db> <adapter>PDO_MSSQL</adapter> <config> <host>localhost</host> <port>1433</port> <username>sa</username> <password>123456<

  • Zend Framework教程之Zend_Config_Ini用法分析

    本文实例讲述了Zend Framework教程之Zend_Config_Ini用法.分享给大家供大家参考,具体如下: Zend_Config_Ini允许开发者通过嵌套的对象属性语法在应用程序中用熟悉的 INI 格式存储和读取配置数据.INI 格式在提供拥有配置数据键的等级结构和配置数据节之间的继承能力方面具有专长.配置数据等级结构通过用点或者句号 (.)分离键值.一个节可以扩展或者通过在节的名称之后带一个冒号(:)和被继承的配置数据的节的名称来从另一个节继承. parse_ini_file Ze

  • Zend Framework框架之Zend_Mail实现发送Email邮件验证功能及解决标题乱码的方法

    本文实例讲述了Zend Framework框架之Zend_Mail实现发送Email邮件验证功能及解决标题乱码的方法.分享给大家供大家参考,具体如下: Zend Framework 里Zend_Mail这个组件用起来还是很方便的..它提供了通用化的编写与发送文本内容的邮件,当然它也兼容MIME标准的多个多个段的邮件消息的功能.Zend_Mail里通过默认的Zend_Mail_Transport_SendMail传输或能过Zend_Mail_Transport_Smtp来发送我们的电子邮件. Ze

  • Zend Framework框架路由机制代码分析

    本文分析了Zend Framework框架路由机制代码.分享给大家供大家参考,具体如下: 在框架中,有关路由的调用关系为: 1.apache的mod_rewrite模块把请求路由到框架的启动脚本,一般是index.php: 2.前端控制器Zend_Controller_Front通过dispatch函数进行请求分发: 3.路由器Zend_Controller_Router_Rewrite通过route函数处理路由,对路由器中已有的路由规则,按照加入顺序的逆序(类似于栈,后进先出)对每个route

  • Zend Framework入门教程之Zend_Db数据库操作详解

    本文实例讲述了Zend Framework中Zend_Db数据库操作方法.分享给大家供大家参考,具体如下: 引言:Zend操作数据库通过Zend_Db_Adapter 它可以连接多种数据库,可以是DB2数据库.MySQli数据库.Oracle数据库.等等. 只需要配置相应的参数就可以了. 下面通过案例来展示一下其连接数据库的过程. 连接mysql数据库 代码: <?php require_once 'Zend/Db.php'; $params = array('host'=>'127.0.0.

  • Zend Framework框架Smarty扩展实现方法

    本文实例讲述了Zend Framework框架Smarty扩展实现方法.分享给大家供大家参考,具体如下: 今天总结一下ZF框架中扩展Smarty模板的方法,在ZF帮助文档中已经有比较详细的介绍,在这我稍微多说一些. 一.将smarty的核心文件包放在lib文件夹下,文件包中要包括(internals/,plugins/,Config_File.class.php,Smarty.class.php,Smarty_Compiler.class.php,debug.tpl). 二.在Zend/View

  • Zend Framework教程之Zend_Config_Xml用法分析

    本文实例讲述了Zend Framework中Zend_Config_Xml用法.分享给大家供大家参考,具体如下: Zend_Config_Xml 让开发者能够存储配置数据到一个简单XML格式并通过嵌入对象属性语法来读取. XML文件的根元素(root element)不相关并可以任意命名.顶级的XML元素和配置数据的节相对应. XML格式通过嵌入XML元素到节一级元素(section-level elements)的下面来支持等级结构组织. 叶一级(leaf-level)的XML元素和配置数据的

  • Zend Framework入门教程之Zend_Mail用法示例

    本文实例讲述了Zend Framework入门教程之Zend_Mail用法.分享给大家供大家参考,具体如下: Zend_Mail组件提供了通用化的功能来创建和发送文本. Zend_Mail通过PHP内建的mail()函数或者直接通过SMTP连接来发送邮件. 一个简单的邮件由收件人.主题.邮件内容以及发件人等内容组成. 步骤如下 1.创建对象 2.设置邮件内容 3.发送 案例: <?php require_once "Zend/Mail.php"; $my_mail = new Z

随机推荐