• 技术文章 >php教程 >PHP开发

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

    黄舟黄舟2016-12-14 10:28:41原创613
    本文实例讲述了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_;
    }

    针对不同的类型,进行不同的处理。


    更多内容请关注PHP中文网(m.sbmmt.com)!



    php入门到就业线上直播课:查看学习

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    千万级数据并发解决方案(理论+实战):点击学习

    Mysql单表千万级数据量的查询优化与性能分析

    Mysql主从原理及其在高并发系统中的应用

    专题推荐:zend server
    上一篇:mysql 命令总结 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 深入理解jQuery.data() 的实现方式• 基于jQuery实现Tabs选项卡自定义插件• 缓存技术详谈—php• php输入流php://input使用浅析• 如何实现phpcms v9
    1/1

    PHP中文网