4
votes

This is the Rob Allen's Quick start Tutorial for Zend Framework beta4.

Error Message:Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for album-table

It seems like it fails trying to make a connection to the db, but I have not found way to tell. It's uses a closure to return an instance from the ServiceManager, but gets the above error message.

module/Album/Module.php

namespace Album;

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()
{

    $albumTable = array(
            'factories' => array(
                    'album-table' => function($sm) {
                        $dbAdapter = $sm->get('db-adapter');
                        $table = new AlbumTable($dbAdapter);
                        return $table;
                    },
            ),
    );      
    return $albumTable;
}
}

namespace Application;

use Zend\Db\Adapter\Adapter as DbAdapter,

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
    public function getServiceConfiguration()
    {
            $factoryDBAdaptor = array(
              'factories' => array(
                 'db-adapter' => function($sm) {
                    $config = $sm->get('config');
                    $config = $config['db'];
                    $dbAdapter = new DbAdapter($config);
                    return $dbAdapter;
                 }, 
              ), 
           );
        return $factoryDBAdaptor;
    }    
}

config\autoload\global.php

return array(
    'db' => array(
        'driver' => 'PDO',
        'dsn'            => 'mysql:dbname=zf2tutorial;hostname=localhost',
        'username'       => 'user',
        'password'       => 'password',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
);
5
Having the same issue...Al-Punk
Hi w2wDev did you solved this issue, in case yes, would be helpful if you could share your solution125369

5 Answers

2
votes

It's related to the fact that Zend Framework's master has changed since Beta 4 and so my beta 4-targeted tutorial no longer works with latest ZF master.

Also, the SM may have previous exceptions, so you should check if there are any previous exceptions as that may show an underlying error.

Update
As of 11th July 2012, my tutorial is now updated for Beta 5. It now uses the Db Adapter's ServiceFactory to create the adapter and so you don't even need to modify Application's Module class any more.

0
votes

Make sure the main Module.php has a reference the getServiceConfiguration(). I had the same problem and had forgotten to include it.

module/Application/Module.php:

<?php
namespace Application;
use Zend\Db\Adapter\Adapter as DbAdapter;
class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.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;
                },
            ),
        );
    }
}
0
votes

update your composer.json file with following line.

"zendframework/zendframework": "dev-master#18c8e223f070deb07c17543ed938b54542aa0ed8"

run following commands you will be good to go.

php composer.phar self-update  
php composer.phar update
php composer.phar install
0
votes

I added the code provided to module.php and it was not executed. I changed the key to Zend\db\Adapter\Adapter and that caused it to execute. However, I received the error Undefined index: db on line $config = $config['db']; because $config does not contain that key.

It seems obvious to me that there is additional code necessary to load the db key into the $config array. Is that true? What and where would that code be? My module.php is:

<?php

namespace Album;

use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\Db\Adapter\Adapter as DbAdapter;

class Module implements ServiceProviderInterface {

    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';
    }

    // Add this method:
    public function getServiceConfig() {
        return array(
            'factories' => array(
                'Zend\db\Adapter\Adapter' => function($sm) {
                    echo PHP_EOL . "SM db-adapter executed." . PHP_EOL;
                    $config = $sm->get('config');
                    $config = $config['db'];
                    $dbAdapter = new DbAdapter($config);
                    return $dbAdapter;
                },
                'Album\Model\AlbumTable' => function($sm) {
                    echo PHP_EOL . "SM AlbumTable executed." . PHP_EOL;
                    $tableGateway = $sm->get('AlbumTableGateway');
                    $table = new AlbumTable($tableGateway);
                    return $table;
                },
                'AlbumTableGateway' => function ($sm) {
                    echo PHP_EOL . "SM AlbumTableGateway executed." . PHP_EOL;
                        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                        $resultSetPrototype = new ResultSet();
                        $resultSetPrototype->setArrayObjectPrototype(new Album());
                        return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }

}

?>
0
votes

Fixed this error by disabling toolbar. Just go to config/autoload/zend-developer-tools.local-development and set toolbar to false.

  'toolbar' => [
            /**
             * Enables or disables the Toolbar.
             *
             * Expects: bool
             * Default: false
             */
            'enabled' => false,