Shail Paras
Shail Paras

Reputation: 1163

zf2 - Unable to resolve service Zend\Db\Adapter\Adapter to a factory

Why i'm getting this error even i have added all the classes already?

Unable to resolve service "Zend\Db\Adapter\Adapter" to a factory; are you certain you provided it during configuration?

Here is my Module.php:

namespace Album;

use Album\Model\Album; 
use Album\Model\AlbumTable; 
use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway;

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 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);
                 },
             ),
         );
    }   

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

UPDATED: FILES - application.config.php:

return [
    // Retrieve list of modules used in this application.
    'modules' => [
        'Zend\Router',
        'Zend\Validator',
        'Application',
        'Album',
        'Blog',
    ],

    // These are various options for the listeners attached to the ModuleManager
    'module_listener_options' => [
        'module_paths' => [
            './module',
            './vendor',
        ],
        'config_glob_paths' => [
            // realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php',
            realpath(__DIR__) . '/autoload/{,*.}{global,local}.php',
        ],


        'config_cache_enabled' => false,

        // The key used to create the configuration cache file name.
        'config_cache_key' => 'application.config.cache',


        'module_map_cache_enabled' => false,

        // The key used to create the class map cache file name.
        'module_map_cache_key' => 'application.module.cache',

        // The path in which to cache merged configuration.
        'cache_dir' => 'data/cache/',


        // 'check_dependencies' => true,
    ],

];

Upvotes: 0

Views: 1675

Answers (2)

Kwido
Kwido

Reputation: 1382

Probaly because the $sm->get('Zend\Db\Adapter\Adapter') is not a service but is a class to set up an Adapter and is not registered within the ServiceManager yet.

Create a factory which builds the Adapter class for you by injecting the Config into it. Then register this AdapterFactory, which returns your adapter, within your serviceConfig. Or you could use the default AdapterFactory of Zend, which uses the 'db' key within your configuration to setup the database adapter.

Registering the Adapter within your Application Module.php:

public function getServiceConfig()
{
    return [
        'factories' => [
            \Zend\Db\Adapter\Adapter::class => \Zend\Db\Adapter\AdapterServiceFactory::class,
        ]
        // rest of your configuration
    ];
}

See the documentation of ZF2 v2.4 - Setting up a database adapter

Upvotes: 0

Purple Hexagon
Purple Hexagon

Reputation: 3578

You may have missed a step from the tutorial you are following.

in config/autoload/global.php add:

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

Upvotes: 1

Related Questions