Alex Rivera | Logout

ServiceNotFoundException in ZendFramework 2, example from Rob Allen

Asked 2012-07-27T12:59:40.050
8

for the past few weeks I have been following ZF2 especially Rob Allen's 'Album' example , I have created the example DB-'zf2tutorial' and example table-'album', which works fine fetching all the items when I use php-mysql, so problems with the data in the DB.

My local.php looks like this config.autoload/local.php:

return array(
    'db' => array
    (
        'driver'         => "Pdo",
        'dsn'            => "mysql:dbname=zf2tutorial;hostname=localhost",
        'username'       => "user", //here I added my valid username 
        'password'       => "password", //here I added my valid password 
        'driver_options' => array
        (
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
        ),
    ),
);

Module.php **module/Album/Model.php

<?php
namespace Album;

use Album\Model\AlbumTable;

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 getServiceConfiguration() 
    {
        return array(

            'factories' => array(

                'album-table' => function($sm) //also tried this 'Album\Model\AlbumTable' =>  function($sm)
                {
                    $dbAdapter = $sm->get('db-adapter');//also tried this $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table =
Edit
Report

2 Answers

0

Just got the same issue.

Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Cache\Model\UniversityStorage

In my case the problem was, that PHPUnit could not find the key Cache\Model\UniversityStorage in the service manager (registry) because it's being added in the Module class of the Cache module and the Cache module was missing in the PHPUnit config file and was also not being loaded:

/module/Application/test/phpunit.config.php

<?php
return array(
    'modules' => array(
        'Application',
        'Cache', // <- was missing
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            '../../../config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            'module',
            'vendor',
        ),
    ),
);

I've added the Cache module to the module list. Now it works.

answered 2013-05-08T16:09:39.213
0

The problem is that within config/autoload/global.php the service is named "Zend\Db\Adapter\Adapter" while within module/Album/Module.php it was searching for "Zend\Db\Adapter" (missing additional \Adapter).

Adding missing "\Adapter" within module/Album/Module.php like this fixed it for me.

                'AlbumTableGateway' => function($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
answered 2013-05-14T13:14:57.773

Your Answer