【问题标题】:Zend Framework: Get "invalid connection option "adapter" error when connecting to a PostgreSQL databaseZend Framework:连接到 PostgreSQL 数据库时出现“无效的连接选项”适配器“错误
【发布时间】:2012-08-13 06:54:30
【问题描述】:

我遇到了一个奇怪的问题,我什至在网上都找不到太多答案。

代码是这样的:

$config = new Zend_Config_Ini('../application/configs/application.ini',
                              APPLICATION_ENV);
$db = Zend_Db::factory($config->resources->db->adapter,
                       $config->resources->db->params->toArray());
$db->setFetchMode(Zend_db::FETCH_ASSOC);
$db->query('set names utf8;');

然后屏幕上出现错误:

An error occurred
Application error
Exception information:

Message: SQLSTATE[08006] [7] invalid connection option "adapter"
Stack trace:

#0 /srv/uhg/ZF/library/Zend/Db/Adapter/Pdo/Pgsql.php(87): Zend_Db_Adapter_Pdo_Abstract->_connect()
#1 /srv/uhg/ZF/library/Zend/Db/Adapter/Abstract.php(459): Zend_Db_Adapter_Pdo_Pgsql->_connect()
#2 /srv/uhg/ZF/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('set names utf8;', Array)
#3 /srv/uhg/workspaces/zpe/library/Tls/Acl.php(72): Zend_Db_Adapter_Pdo_Abstract->query('set names utf8;')
#4 /srv/uhg/workspaces/zpe/application/controllers/IndexController.php(12): Tls_Acl->__construct('developer')
#5 /srv/uhg/ZF/library/Zend/Controller/Action.php(133): IndexController->init()
#6 /srv/uhg/ZF/library/Zend/Controller/Dispatcher/Standard.php(268): Zend_Controller_Action->__construct(Object(Zend_Controller_Request_Http ), Object(Zend_Controller_Response_Http), Array)
#7 /srv/uhg/ZF/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#8 /srv/uhg/ZF/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#9 /srv/uhg/ZF/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#10 /srv/uhg/workspaces/zpe/public/index.php(29): Zend_Application->run()

我还试图省略我在 $config 中得到的内容:

PDO_PGSQL:配置的东西

Array ( [adapter] => PDO_PGSQL
        [host] => localhost 
        [username] => postgres
        [password] =>
        [dbname] => uhg
        [default] => 1 )

当我在网上搜索时,有人建议我应该取消设置$config->resources->db->adapter(当然是在将它的值存储在某个地方之后),但是当我尝试这样做时,它表明我不能。

这似乎只发生在 postgresql 中,因为它适用于 MySQL 数据库。

我对 Posgresql 很陌生,这可能是 Postgresql 的问题吗?

请有人帮忙。

干杯。

PS:

对于$db,它导出(我将真实的db名称替换为xx-xxx):

object(Zend_Db_Adapter_Pdo_Pgsql)#91 (12) { ["_pdoType":protected]=> string(5) "pgsql" ["_numericDataTypes":protected]=> array(12) { [0]=> int( 0) [1]=> int(1) [2]=> int(2) ["INTEGER"]=> int(0) ["SERIAL"]=> int(0) ["SMALLINT"]=> int (0) ["BIGINT"]=> int(1) ["BIGSERIAL"]=> int(1) ["DECIMAL"]=> int(2) ["DOUBLE PRECISION"]=> int(2) [" NUMERIC"]=> int(2) ["REAL"]=> int(2) } ["_defaultStmtClass":protected]=> string(21) "Zend_Db_Statement_Pdo" ["_config":protected]=> array(10) { ["适配器"]=> string(9) "Pdo_Pgsql" ["host"]=> string(9) "localhost" ["username"]=> string(8) "postgres" ["password"]=> string(0) "" ["dbname"]=> string(3) "xxx-xxx" ["default"]=> string(1) "1" ["charset"]=> NULL ["persistent"]= > bool(false) ["options"]=> array(3) { ["caseFolding"]=> int(0) ["autoQuoteIdentifiers"]=> bool(true) ["fetchMode"]=> int(2) } ["driver_options"]=> array(0) { } } ["_fetchMode":protected]=> int(2) ["_profiler":protected]=> object(Zend_Db_Profiler)#92 (4) { ["_queryProfiles ":受保护]=> 数组( 0) { } ["_enabled":protected]=> bool(false) ["_filterElapsedSecs":protected]=> NULL ["_filterTypes":protected]=> NULL } ["_defaultProfilerClass":protected]=> string(16 ) "Zend_Db_Profiler" ["_connection":protected]=> NULL ["_caseFolding":protected]=> int(0) ["_autoQuoteIdentifiers":protected]=> bool(true) ["_allowSerialization":protected]=> bool (true) ["_autoReconnectOnUnserialize":protected]=> bool(false) }

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!

感谢大家的帮助,我找到了解决这个问题的方法:

$db = new Zend_Db_Adapter_Pdo_Pgsql(array(
             'host' => 'localhost',  
            'username' => 'postgres',  
            'password' => '',  
            'dbname' => 'uhg'        
 ));

【问题讨论】:

  • 适配器名称的正确参数是“Pdo_Pgsql”。您还可以显示$config->resources->db 中的内容吗(当然要屏蔽任何敏感信息)?
  • @MIlen,嗨,看来即使我将适配器名称更改为您建议的方式,它也不会改变任何内容。
  • @Milen,对于 $config->resources->db,它会导出一个对象:我会将其粘贴到上面的问题帖子中。
  • Offtopic:“设置名称 utf8;”不是有效的 PostgreSQL 查询,它将失败。使用“SET client_encoding = utf8;”如果默认设置不适合,则为当前会话设置正确的编码。

标签: database zend-framework postgresql connection


【解决方案1】:

我想我可以看到正在发生的事情,但不清楚如何或为什么。

“适配器”选项似乎传递给 PostgreSQL 连接本身,显然它不支持。

【讨论】:

  • 如果您找到了解决方案,我认为您应该添加自己的答案并接受它。
猜你喜欢
  • 2019-01-08
  • 1970-01-01
  • 2018-06-04
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-20
相关资源
最近更新 更多