I started my adventure learning Zend Framework 2 today. I decided to begin with Rob Allen’s
tutorial at
http://akrabat.com/zend-framework-2-tutorial/. I set up my PC with XAMPP 1.7.7 and installed Zend
Framework 2 beta 4. I made excellent progress with the tutorial until I reached
"Listing albums" using http://zf2-tutorial.localhost/album and I kept receiving the following errors:
Zend\ServiceManager\Exception\ServiceNotCreatedException
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for album-table
Zend\ServiceManager\Exception\ServiceNotCreatedException
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for db-adapter
Zend\Db\Adapter\Exception\InvalidArgumentException
The supplied or instantiated driver object does not implement Zend\Db\Adapter\Driver\DriverInterface
It appears that my application is not instantiating my PDO
driver correctly. I investigated the function getServiceConfiguration() in
module/Application/Module.php:
public function getServiceConfiguration()
{
return array(
'factories' => array(
'db-adapter' => function($sm) {
$config = $sm->get('config');
$config = $config['db'];
$dbAdapter = new DbAdapter($config);
return $dbAdapter;
},
),
);
}
The factory is not passing the 'db' array from the merged configurations correctly. I fixed the exceptions by changing the getServiceConfiguration() to:public function getServiceConfiguration()
{
return array(
'factories' => array(
'db-adapter' => function($sm) {
$config = $sm->get('config');
$config = $config['db'];
$config = array(
'driver' => $config['driver'],
'dsn' => $config['dsn'],
'username' => $config['username'],
'password' => $config['password'],
'driver_options' => $config['driver_options']
);
$dbAdapter = new DbAdapter($config);
return $dbAdapter;
},
),
);
}
Now I also downloaded Rob Allen's github repo of the tutorial, and the repo threw the same exceptions as my original getServiceConfiguration(). Am I doing something wrong? Is my solution correct? How could I create a more elegant solution?