Database XML Config xml file 如:
<database>
<adapter>Pdo_Oci</adapter>
<!-- web server 上 tnsname.ora 中的 tnsname -->
<dbname>q78=</dbname>
<username>qsC8</username>
<password>qsC8</password>
<charset>utf8</charset>
</database>
Zend Framework 1.0+ 运行OK,升级到 ZF1.5 后,用原来的配置档,无法连到 Oracle,出现如下错误:
Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12533: TNS:illegal ADDRESS parameters (ext/pdo_oci/oci_driver.c:579)' in C:/Program Files/Zend/Core For Oracle/ZendFramework/library/Zend/Db/Adapter/Pdo/Abstract.php:131 Stack trace: #0 C:/Program Files/Zend/Core For Oracle/ZendFramework/library/Zend/Db/Adapter/Abstract.php(390): Zend_Db_Adapter_Pdo_Abstract->_connect() #1
经过一番折腾,找出原因如下:
在 Zend Framework 1.5 的版本中 Zend/Db/Adapter/Pdo/Oci.php 的 protected function _dsn() 做了更改, 1.0+ 中的程式代码如下:
[phpcode]protected function _dsn()
{
// baseline of DSN parts
$dsn = $this->_config;
$tns = 'dbname=';
if (isset($dsn['host'])) {
$tns .= '//' . $dsn['host'];
if (isset($dsn['port'])) {
$tns .= ':' . $dsn['port'];
}
$tns .= '/';
}
$tns .= $dsn['dbname'];
if (isset($dsn['charset']))
{
$tns .= ';charset=' . $dsn['charset'];
}
return $this->_pdoType . ':' . $tns;
}[/phpcode]
1.5 中 protected _dsn() function 如下:
protected function _dsn()
{
// baseline of DSN parts
$dsn = $this->_config;
$tns = 'dbname=(DEscrīptION=';
if (isset($dsn['host'])) {
$tns .= '(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=' . $dsn['host'] . ')';
if (isset($dsn['port'])) {
$tns .= '(PORT=' . $dsn['port'] . ')';
} else {
$tns .= '(PORT=1521)';
}
$tns .= '))';
}
$tns .= '(CONNECT_DATA=(SID=' . $dsn['dbname'] . ')))';
if (isset($dsn['charset']))
{
$tns .= ';charset=' . $dsn['charset'];
}
return $this->_pdoType . ':' . $tns;
}
解决以上错误必须修改 Database Config xml file
1. 必须指定 host
2. dbname 必须为指定 host(database server name or ip address)上 database 的 sid(service name)
修改后适合 ZF 1.5 xml config file 内容如下:
<database>
<adapter>Pdo_Oci</adapter>
<!-- hostname or IP address-->
<host>192.168.0.200</host>
<!-- database SID or Service Name -->
<dbname>dbsid</dbname>
<username>qsC8</username>
<password>qsC8</password>
<charset>utf8</charset>
</database>
其它方法:
不用 Pdo_Oci adapater 直接用 Oracle Adapter 就可以不用改动了.
<database>
<adapter>Pdo_Oci</adapter>
<!-- web server 上 tnsname.ora 中的 tnsname -->
<dbname>q78=</dbname>
<username>qsC8</username>
<password>qsC8</password>
<charset>utf8</charset>
</database>
Zend Framework 1.0+ 运行OK,升级到 ZF1.5 后,用原来的配置档,无法连到 Oracle,出现如下错误:
Fatal error: Uncaught exception 'Zend_Db_Adapter_Exception' with message 'SQLSTATE[HY000]: pdo_oci_handle_factory: ORA-12533: TNS:illegal ADDRESS parameters (ext/pdo_oci/oci_driver.c:579)' in C:/Program Files/Zend/Core For Oracle/ZendFramework/library/Zend/Db/Adapter/Pdo/Abstract.php:131 Stack trace: #0 C:/Program Files/Zend/Core For Oracle/ZendFramework/library/Zend/Db/Adapter/Abstract.php(390): Zend_Db_Adapter_Pdo_Abstract->_connect() #1
经过一番折腾,找出原因如下:
在 Zend Framework 1.5 的版本中 Zend/Db/Adapter/Pdo/Oci.php 的 protected function _dsn() 做了更改, 1.0+ 中的程式代码如下:
[phpcode]protected function _dsn()
{
// baseline of DSN parts
$dsn = $this->_config;
$tns = 'dbname=';
if (isset($dsn['host'])) {
$tns .= '//' . $dsn['host'];
if (isset($dsn['port'])) {
$tns .= ':' . $dsn['port'];
}
$tns .= '/';
}
$tns .= $dsn['dbname'];
if (isset($dsn['charset']))
{
$tns .= ';charset=' . $dsn['charset'];
}
return $this->_pdoType . ':' . $tns;
}[/phpcode]
1.5 中 protected _dsn() function 如下:
protected function _dsn()
{
// baseline of DSN parts
$dsn = $this->_config;
$tns = 'dbname=(DEscrīptION=';
if (isset($dsn['host'])) {
$tns .= '(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=' . $dsn['host'] . ')';
if (isset($dsn['port'])) {
$tns .= '(PORT=' . $dsn['port'] . ')';
} else {
$tns .= '(PORT=1521)';
}
$tns .= '))';
}
$tns .= '(CONNECT_DATA=(SID=' . $dsn['dbname'] . ')))';
if (isset($dsn['charset']))
{
$tns .= ';charset=' . $dsn['charset'];
}
return $this->_pdoType . ':' . $tns;
}
解决以上错误必须修改 Database Config xml file
1. 必须指定 host
2. dbname 必须为指定 host(database server name or ip address)上 database 的 sid(service name)
修改后适合 ZF 1.5 xml config file 内容如下:
<database>
<adapter>Pdo_Oci</adapter>
<!-- hostname or IP address-->
<host>192.168.0.200</host>
<!-- database SID or Service Name -->
<dbname>dbsid</dbname>
<username>qsC8</username>
<password>qsC8</password>
<charset>utf8</charset>
</database>
其它方法:
不用 Pdo_Oci adapater 直接用 Oracle Adapter 就可以不用改动了.