zend框架实现支持sql server的操作方法
本文实例讲述了zend框架实现支持sqlserver的操作方法。分享给大家供大家参考,具体如下:
1.修改Zend/Db/Adapter/Pdo/Abstract.php中的connect方法
protectedfunction_connect() { //ifwealreadyhaveaPDOobject,noneedtore-connect. if($this->_connection){ return; } //getthedsnfirst,becausesomeadaptersalterthe$_pdoType $dsn=$this->_dsn(); //checkforPDOextension if(!extension_loaded('pdo')){ /** *[url=home.php?mod=space&uid=86763]@see[/url]Zend_Db_Adapter_Exception */ require_once'Zend/Db/Adapter/Exception.php'; thrownewZend_Db_Adapter_Exception('ThePDOextensionisrequiredforthisadapterbuttheextensionisnotloaded'); } //checkthePDOdriverisavailable if(!in_array($this->_pdoType,PDO::getAvailableDrivers())){ /** *@seeZend_Db_Adapter_Exception */ require_once'Zend/Db/Adapter/Exception.php'; thrownewZend_Db_Adapter_Exception('The'.$this->_pdoType.'driverisnotcurrentlyinstalled'); } //createPDOconnection $q=$this->_profiler->queryStart('connect',Zend_Db_Profiler::CONNECT); //addthepersistenceflagifwefinditinourconfigarray 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=newPDO("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=newPDO( $dsn, $this->_config['username'], $this->_config['password'], $this->_config['driver_options'] ); $this->_profiler->queryEnd($q); } //setthePDOconnectiontoperformcase-foldingonarraykeys,ornot $this->_connection->setAttribute(PDO::ATTR_CASE,$this->_caseFolding); //alwaysuseexceptions. $this->_connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); }catch(PDOException$e){ /** *@seeZend_Db_Adapter_Exception */ require_once'Zend/Db/Adapter/Exception.php'; thrownewZend_Db_Adapter_Exception($e->getMessage()); } }
这里针对linux和windows提供两种连接方式。
2.mssql.php中的为protected$_pdoType='sqlsrv';
protectedfunction_dsn() { //baselineofDSNparts $dsn=$this->_config; //don'tpasstheusernameandpasswordintheDSN 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']); } //thisdriversupportsmultipleDSNprefixes //@seehttp://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']); } //useallremainingpartsintheDSN foreach($dsnas$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>
期间遇到中文乱码问题
functionconvert2utf8($string) { $config=$this->getCfg(); $pdoType=$config->db->config->pdoType; if($pdoType=='dblib'){ returniconv("gbk","utf-8",$string); }elseif($pdoType=='sqlsrv'){ returnmb_convert_encoding($string,"UTF-8","auto"); } } functionconvert2gbk($string) { $config=$this->getCfg(); $pdoType=$config->db->config->pdoType; if($pdoType=='dblib'){ returniconv("utf-8","gbk",$string); }elseif($pdoType=='sqlsrv'){ returnmb_convert_encoding($string,"GBK","auto"); } } protectedfunction&getCfg(){ if($this->cfg_===null){ $registry=Zend_Registry::getInstance(); $this->cfg_=$registry->get('web_config'); } return$this->cfg_; }
针对不同的类型,进行不同的处理。
更多关于zend相关内容感兴趣的读者可查看本站专题:《ZendFrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于ZendFramework框架的PHP程序设计有所帮助。