zend支持sql server
2013-11-22 17:28
387 查看
1.修改Zend下Db下Adapter下Pdo下Abstract.php中的connect方法
这里针对linux和windows提供两种连接方式。
2.mssql.php 中的为 protected $_pdoType = 'sqlsrv';
3.ZF 的web.xml 数据库配置文件改成:
期间遇到中文乱码问题
针对不同的类型,进行不同的处理。
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')) { /** * @see 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框架实现支持sql server的操作方法
- zend框架实现支持sql server的操作方法
- --切分(Split),使用SQL Server 2005对XQuery的支持:
- SQL Server 2008 Spatial, PostgreSQL/PostGIS 1.3-1.4, MySQL 5-6 三种支持空间数据系统比较
- SQL Server 中的XML支持,查询返回XML,Web访问SQL取XML形式结果
- “此版本的 SQL Server 不支持用户实例登录标志。该连接将关闭”的处理
- SQL Server 2005 中的 Unicode 支持
- 解决“此版本的 SQL Server 不支持用户实例登录标志。该连接将关闭”问题,完整综合版。
- sql server 发布订阅报错-该伙伴事务管理器已经禁止了它对远程/网络事务的支持
- MS SQL入门基础:SQL Server 2000 与Internet-支持Web 应用的新特性
- SQL Server 2008 导入.mdf文件时遇 (数据库 的版本为 661,此服务器支持 655 版错误),完美解决
- 使用的 SQL Server 版本不支持数据类型“datetime2”.
- SQL Server 2008支持将数据导出为脚本
- PowerDesigner支持自动生成含SQL Server 2000的表和列注释的角本
- sql server 备份失败[上加载的介质已格式化为支持 1 个介质簇,但根据指定的备份设备,应支持 2 个介质簇]
- [SQL Server]2008版本的bcp,bulk insert命令不支持UTF8文件.2005版支持.
- 无法升级数据库....因为此版本的 SQL Server 不支持该数据库的非发布版本(539) 解决方案
- 深入探讨SQL Server 2000对XML的支持(四)
- 使用的 SQL Server 版本不支持数据类型“datetime2”解决办法
- SQL Server 2005 SP3 支持的SMS/SCCM版本