如何让 Zend Framework 2 beta4 中的服务管理器为专辑表创建实例? [英] How do I get the Service Manager in Zend Framework 2 beta4 to create an instance for album-table?

查看:23
本文介绍了如何让 Zend Framework 2 beta4 中的服务管理器为专辑表创建实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

错误消息:Zend\ServiceManager\ServiceManager::get 无法为专辑表获取或创建实例

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

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

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.php

module/Album/Module.php

命名空间相册;

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,

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

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

推荐答案

这与 Zend Framework 的母版自 Beta 4 以来发生了变化有关,因此我的 Beta 4 目标教程不再适用于最新的 ZF 母版.

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.

此外,SM 可能有以前的异常,因此您应该检查是否有任何以前的异常,因为这可能表明潜在错误.

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

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

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.

这篇关于如何让 Zend Framework 2 beta4 中的服务管理器为专辑表创建实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆