1
votes

i'm following this tutorial but got error Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Zend\Db\Adapter\Adapter

i've googling and tried all solution but no luck. please helppppp i'm depressed :|

FYI : i'm using this skeleton https://github.com/zendframework/ZendSkeletonApplication and go. i didn't install zend.

module.php namespace Album;

// Add these import statements:
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\Tabl`enter code here`eGateway;

class Module
{
 public function getAutoloaderConfig()
 {
     return array(
         'Zend\Loader\ClassMapAutoloader' => array(
             __DIR__ . '/autoload_classmap.php',
         ),
         'Zend\Loader\StandardAutoloader' => array(
             'namespaces' => array(
                 __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
             ),
         ),
     );
 }

 public function getConfig()
 {
     return include __DIR__ . '/config/module.config.php';
 }

 public function getServiceConfig()
 {
     return array(
         'factories' => array(
             'Album\Model\AlbumTable' =>  function($sm) {
                 $tableGateway = $sm->get('AlbumTableGateway');
                 $table = new AlbumTable($tableGateway);
                 return $table;
             },
             'AlbumTableGateway' => function ($sm) {
                 $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                 $resultSetPrototype = new ResultSet();
                 $resultSetPrototype->setArrayObjectPrototype(new Album());
                 return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
             },
         ),
     );
 }

}

global.php

return array(
 'db' => array(
     'driver'         => 'Pdo',
     'dsn'            => 'mysql:dbname=aaa;host=aaa',
     'driver_options' => array(
         PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
     ),
 ),
 'service_manager' => array(
     'factories' => array(
         'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
     ),
 ),

);

i read & tried these : ZF2 - get was unable to fetch or create an instance for getAlbumTable

ServiceNotFoundException in ZendFramework 2, example from Rob Allen

always end up without clarity

4

4 Answers

3
votes

This is because of DB configuration error, to solve this you have to configure DB in global.php in main config folder.Code is given below, Copy paste and Just change the db name and password,

return array(
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=zf2tutorial;host=localhost',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
        'username'       => 'root',
        'password'       => ''
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter'
                    => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);
1
votes

This will work if you are getting problem on Ibm I series Server.

The cause of the problem is that IBMi cannot handle relative paths in the config. I resolved it on my machine with the following

'config_glob_paths' => array( '/www/local/zf2/config/autoload/{,*.}{global,local}.php', ),

ie set 'config_glob_paths' to the full path instead of the relative path in application.config.php.

Note: if you are getting the error on local then pls check configruation file.

1
votes

The problem is with the following line in your module.php

$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');

The db configuration information in your global.php can not recognized by your program. As a resolve, you can transfer the configuration you have written on global.php to module.config.php.

My module.config.php is as follows:

<?php
return array(
'controllers' => array(
    'invokables' => array(
        'Album\Controller\Album' => 'Album\Controller\AlbumController',
    ),
),

'db' => array(
    'driver'         => 'Pdo',
    'dsn'            => 'mysql:dbname=zend;host=localhost',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
),

// The following section is new and should be added to your file
'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'album' => __DIR__ . '/../view',
    ),
),
); 

And my getServiceConfig() under Module.php is as below:

 public function getServiceConfig()
  {
    return array(
        'factories' => array( 

            'db' => function($sm) {
                echo PHP_EOL . "SM db-adapter executed." . PHP_EOL;
                $config = $sm->get('config');
                $config = $config['db'];
                //print_r($config);
                //exit();
                $dbAdapter = new Adapter($config);
                return $dbAdapter;
            },
            'Album\Model\AlbumTable' =>  function($sm) {
                $tableGateway = $sm->get('AlbumTableGateway');
                $table = new AlbumTable($tableGateway);
                return $table;
            },
            'AlbumTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('db');
                //print_r($dbAdapter);
                //exit();             
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Album());
                return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
            },
        ),
    );
}

After that everything was working.

Note: For some reason global.php was not read by Zend2 in my case.(Somebody needs to research further with that I suggest)local.php was still read. So I am still keeping my username and password information in local.php. Hope this will help you to get rid of this uncomfortable error.

0
votes

I just added the code below in "my_project / config / autoload / global.php" and it worked perfectly.

'service_manager' => array(
    'factories' => array(
        'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
    ),

I am using Oracle and let "username / password" in "local.php".

Thank you!