I’m trying to setup a new web-application for learning purposes using the newest technologies: apache 2.4, PHP 5.4 and PostgreSQL 9.1
I’ve chosen to develop using Zend Framework 2.
In my main module I have defined this method:
public function getServiceConfiguration() {
return array(
'factories' => array(
'adapter' => function ($sm) {
$config = $sm->get('config');
$adapter = new Adapter($config['db']);
return $adapter;
}
),
);
}
$config[‘db’] is defined in my autoload/global.php, which contains:
return array(
'db' => array(
'driver' => 'PDO',
'dsn' => 'pgsql:host=localhost;port=5436;user=root;password=myrootpwd',
)
);
but when I try to call $serviceManager->get(‘adapter’) I get an exception saying:
Fatal error: Uncaught exception ‘Zend\Db\Adapter\Exception\InvalidArgumentException’ with message ‘The supplied or instantiated driver object does not implement Zend\Db\Adapter\Driver\DriverInterface’ in C:\Program Files\626Suite\application\library\Zend\ServiceManager\ServiceManager.php on line 294
Zend\Db\Adapter\Exception\InvalidArgumentException: The supplied or instantiated driver object does not implement Zend\Db\Adapter\Driver\DriverInterface in C:\Program Files\626Suite\application\library\Zend\Db\Adapter\Adapter.php on line 80
Call Stack:
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
EDIT:
I tried to modify the factory as:
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;
}
),
);
}
And I first got an error saying that database "root" does not exists, but after changing my configuration to:
return array(
'db' => array(
'driver' => 'PDO',
'dsn' => 'pgsql:host=localhost;port=5436;dbname=postgres;user=root;password=myrootpwd',
)
);
I got back to getting the exception I described before.
In the end, I’ve been able to solve this problem too.
The solution was to update the currently used version of Zend Framework 2 from beta4 to the latest master downloaded directly from github.
I suppose there was a problem somewhere in Zend code itself, since now it is working properly without having changed anything in my code or in my settings.