This article illustrates how the Zend Framework implementation supports SQL Server. Share to everyone for your reference, specific as follows:
1. Modify the Connect method in 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 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 [/ur
L] zend_db_adapter_exception * require_once ' zend/db/adapter/exception.php ';
throw new Zend_db_adapter_exception (' The PDO extension is required to 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 my our config array if isset ($this->_config[' persistent ') && ($th
is->_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[' Ho St ']. ";D
Atabase = ". $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 (P
Do::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/except
Ion.php ';
throw new Zend_db_adapter_exception ($e->getmessage ());
}
}
Here are two ways to connect with Linux and Windows.
2.mssql.php in the 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 ($dsn [' Pdotype ']) {switch (Strtolower ($dsn [' Pdotype '])) {case ' FreeTDS ': Case ' isset ':
$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;
}
The 3.ZF web.xml database configuration file is changed to:
<db>
<adapter>PDO_MSSQL</adapter>
<config>
Encountered Chinese garbled problem during
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 (); c23/> $this->cfg_ = $registry->get (' web_config ');
}
return $this->cfg_;
}
For different types, do different processing.
More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on the Zend Framework.