How do I get the Service Manager in Zend Framework 2 beta4 to create an instance for album-table?

馋奶兔 提交于 2019-12-10 08:00:57

问题


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\''
        ),
    ),
);

回答1:


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.




回答2:


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



回答3:


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



回答4:


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

}

?>



回答5:


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,


来源:https://stackoverflow.com/questions/11355126/how-do-i-get-the-service-manager-in-zend-framework-2-beta4-to-create-an-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!