【问题标题】:How do I get the Service Manager in Zend Framework 2 beta4 to create an instance for album-table?如何让 Zend Framework 2 beta4 中的服务管理器为专辑表创建一个实例?
【发布时间】:2012-07-06 11:52:30
【问题描述】:

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

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

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

模块/相册/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,

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

【问题讨论】:

  • 遇到同样的问题...
  • 嗨 w2wDev 你解决了这个问题吗,如果是的话,如果你能分享你的解决方案会很有帮助

标签: zend-framework2


【解决方案1】:

这与 Zend Framework 的 master 自 Beta 4 以来发生了变化有关,因此我的针对 beta 4 的教程不再适用于最新的 ZF master。

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

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

【讨论】:

  • 显然问题出在 $dbAdapter = $sm->get('db-adapter');行
【解决方案2】:

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

【讨论】:

  • @Armand 我得到了同样的错误,w2wDev 发布的内容。有什么建议。我的主要 Module.php 引用了您提到的 getServiceConfiguration() ,但仍然出现相同的错误。任何帮助将不胜感激
【解决方案3】:

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

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

运行以下命令就可以了。

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

【讨论】:

    【解决方案4】:

    我添加了提供给 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);
                    },
                ),
            );
        }
    
    }
    
    ?>
    

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多