我正在尝试使用最新技术设置一个新的网络应用程序用于学习目的: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('adapter') 我得到一个异常说:
致命错误:未捕获异常 'Zend\Db\Adapter\Exception\InvalidArgumentException' 并在 C:\Program Files\626Suite\application\library 中显示消息“提供或实例化的驱动程序对象未实现 Zend\Db\Adapter\Driver\DriverInterface” \Zend\ServiceManager\ServiceManager.php 在第 294 行
Zend\Db\Adapter\Exception\InvalidArgumentException:提供或实例化的驱动程序对象没有实现 C:\Program Files\626Suite\application\library\Zend\Db\Adapter\Adapter.php 中的 Zend\Db\Adapter\Driver\DriverInterface在第 80 行
调用堆栈:
0.0015 121600 1. {main}() 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:\Program Files\626Suite\application\module\Age\Module.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',
)
);
我回到了我之前描述的异常。