본 글의 예시에서는 SQL Server를 지원하기 위한 zend Framework의 동작 방식을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 자세한 내용은 다음과 같습니다.
1. Zend/Db/Adapter/Pdo/Abstract.php에서 연결 방법을 수정합니다.
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는 보호됩니다 $_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 중국어 홈페이지(www.php.cn)를 주목해주세요!