zend架構實現支援sql server的操作方法_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了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程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.