本文实例讲述了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 framework框架的php程序设计有所帮助。
更多zend框架实现支持sql server的操作方法。