我尝试使用最新技术设置一个新的网络应用程序用于学习目的: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' with message'提供的或实例化的驱动程序对象未实现Zend \ Db \ Adapter \ Driver \ DriverInterface'在第294行的C:\ Program Files \ 626Suite \ application \ library \ Zend \ ServiceManager \ ServiceManager.php
Zend \ Db \ Adapter \ Exception \ InvalidArgumentException:提供的或实例化的驱动程序对象未在C:\ Program Files \ 626Suite \ application \ library \ Zend \ Db \ Adapter \中实现Zend \ Db \ Adapter \ Driver \ DriverInterface第80行的Adapter.php
调用堆栈:
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(类Closure,string(7),string(7))C:\ Program Files \ 626Suite \ application \ library \ Zend \ ServiceManager \ ServiceManager.php:353
0.5701 936672 5. call_user_func(类Closure,类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}(类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(类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',
)
);
我回到了我之前描述的例外。
答案 0 :(得分:5)
以下适用于我:
/config/autoload/database.global.php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => "pgsql:host=127.0.0.1;dbname=mydb",
'username' => 'username',
'password' => 'mypass',
),
);
/module/Application/Module.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));
答案 1 :(得分:1)
最后,我也能够解决这个问题。
解决方案是将当前使用的Zend Framework 2版本从beta4更新为直接从github下载的最新版本。
我认为Zend代码本身存在问题,因为现在它在我的代码或设置中没有更改任何内容的情况下正常工作。