Zend Framework 2 无法创建 PostgreSQL 数据库适配器


Zend Framework 2 cannot create PostgreSQL db adapter

我正在尝试使用最新技术设置一个新的Web应用程序用于学习目的:apache 2.4,PHP 5.4和PostgreSQL 9.1

我选择使用 Zend Framework 2 进行开发。

在我的主模块中,我定义了此方法:

public function getServiceConfiguration() {
    return array(
        'factories' => array(
            'adapter' => function ($sm) {
                $config = $sm->get('config');
                $adapter = new Adapter($config['db']);
                return $adapter;
            }
        ),
    );
}

$config['db'] 在我的 autoload/global.php 中定义,其中包含:

return array(
    'db' => array(
        'driver' => 'PDO',
        'dsn' => 'pgsql:host=localhost;port=5436;user=root;password=myrootpwd',
    )
);

但是当我尝试调用$serviceManager->get('适配器')时,我得到一个异常说:

致命错误:未捕获的异常"Zend''Db''适配器''异常''无效参数异常",消息"提供或实例化的驱动程序对象未在第 294 行的 C:''Program Files''626Suite''application''library''Zend''ServiceManager''ServiceManager.php中实现 Zend''Db''适配器''驱动程序''驱动程序接口"

Zend''Db''Adapter''Exception''

InvalidArgumentException:提供或实例化的驱动程序对象在第 80 行的 C:''Program Files''626Suite''application''library''Zend''Db''Adapter''Adapter.php 中实现 Zend''Db''Adapter''DriverInterface

调用堆栈:
0.0015 121600 1.{主要}() C:''Program Files''626Suite''application''data''script''install.php:0
0.5699 936080 2.Zend''ServiceManager''ServiceManager->get(string(7), ???)C:''Program Files''626Suite''application''data''script''install.php:7
0.5700 936440 3.Zend''ServiceManager''ServiceManager->create(array(2)) C:''Program Files''626Suite''application''library''Zend''ServiceManager''ServiceManager.php:277
0.5701 936520 4.Zend''ServiceManager''ServiceManager->createServiceViaCallback(class Closure, string(7), string(7)) C:''Program Files''626Suite''application''library''Zend''ServiceManager''ServiceManager.php:353
0.5701 936672 5.call_user_func(class Closure, class Zend''ServiceManager''ServiceManager, string(7), string(7)) C:''Program Files''626Suite''application''library''Zend''ServiceManager''ServiceManager.php:543
0.5701 936696 6.Age''Module->Age{closure}(class Zend''ServiceManager''ServiceManager, string(7), string(7)) C:''Program Files''626Suite''application''library''Zend''ServiceManager''ServiceManager.php:543
0.6047 1024184 7.Zend''Db''Adapter''Adapter->__construct(class Zend''Config''Config, ???, ???)C:''程序文件''626套件''应用程序''模块''年龄''模块.php:43

编辑:

我尝试将工厂修改为:

public function getServiceConfiguration() {
    return array(
        'factories' => array(
            'adapter' => function ($sm) {
                $config = $sm->get('config');
                $PDO = new 'PDO($config['db']['dsn']);
                $adapter = new Adapter($PDO);
                return $adapter;
            }
        ),
    );
}

我首先收到一个错误,说数据库"root"不存在,但是在将我的配置更改为:

return array(
    'db' => array(
        'driver' => 'PDO',
        'dsn' => 'pgsql:host=localhost;port=5436;dbname=postgres;user=root;password=myrootpwd',
    )
);

又回到了我之前描述的异常。

以下内容对我有用:

/

config/autoload/database.global.php

return array(
'db' => array(
    'driver'    => 'Pdo',
    'dsn'       => "pgsql:host=127.0.0.1;dbname=mydb",
    'username'  => 'username',
    'password'  => 'mypass',
),
);
/模块/

应用程序/模块.php

public function getServiceConfiguration()
{
    return array(
        'factories' => array(
            'db_adapter' =>  function($sm) {
                $config = $sm->get('Configuration');
                $dbAdapter = new 'Zend'Db'Adapter'Adapter($config['db']);
                return $dbAdapter;
            }
        ),
    );
}

然后你可以像这样使用它:

$sql = "SELECT * FROM table WHERE field=?";
$statement = $this->adapter->query($sql);
$res =  $statement->execute(array($var));

最后,我也能够解决这个问题。

解决方案是将当前使用的 Zend Framework 2 版本从 beta4 更新到直接从 github 下载的最新主版本。

我想 Zend 代码本身的某个地方存在问题,因为现在它可以正常工作,而无需更改我的代码或设置中的任何内容。