如何在Zend Framework 2 beta4中获取Service Manager来为album-table创建实例?

时间:2012-07-06 02:36:40

标签: zend-framework2

这是Rob Allen的Zend Framework beta4快速入门教程。

错误消息:Zend \ ServiceManager \ ServiceManager :: get无法获取或创建album-table的实例

它似乎无法尝试建立与数据库的连接,但我还没有找到方法来判断。它使用一个闭包从ServiceManager返回一个实例,但得到上面的错误信息。

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

命名空间应用程序;

使用Zend \ Db \ Adapter \ Adapter作为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;
    }    
}

设置\自动加载\ 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 个答案:

答案 0 :(得分:2)

这与Zend Framework的主人自Beta 4以来的变化有关,因此我的beta 4目标教程不再适用于最新的ZF主人。

此外,SM可能有先前的异常,因此您应检查是否存在任何先前的异常,因为这可能会显示潜在的错误。

<强>更新
截至2012年7月11日,我的tutorial现已更新为Beta 5.它现在使用Db Adapter的ServiceFactory来创建适配器,因此您甚至不需要再修改Application的Module类。

答案 1 :(得分:0)

确保主Module.php具有getServiceConfiguration()的引用。我有同样的问题,忘了把它包括在内。

模块/应用/ 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;
                },
            ),
        );
    }
}

答案 2 :(得分:0)

使用以下行更新您的composer.json文件。

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

运行以下命令,你会很高兴。

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

答案 3 :(得分:0)

我添加了提供给module.php的代码,但没有执行。我将密钥更改为Zend \ db \ Adapter \ Adapter,导致它执行。但是,我收到错误Undefined index:db on line $ config = $ config ['db'];因为$ config不包含该密钥。

对我来说,显然需要额外的代码才能将db密钥加载到$ config数组中。真的吗?该代码的内容和位置是什么?我的module.php是:

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

}

?>

答案 4 :(得分:0)

通过禁用工具栏修复了此错误。只需转到config/autoload/zend-developer-tools.local-development并将工具栏设置为false。

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