0

我收到一个错误: Zend\ServiceManager\Exception\ServiceNotCreatedException: An exception was raise while creating "Zend\Db\Adapter\Adapter"; 没有返回实例。

往下追溯,我发现 Zend\Db\Adapter\Adapter 出现在 global.php 中。但是, global.php 从未被调用过,或者至少在发生错误的 module.php getserviceconfig() 之前没有被调用过。testconfig.php.dist 和 application.config.php 都将 global.php 呈现给侦听器定义。但是,global.php 永远不会执行。

有人可以告诉我为什么吗?定义如下。

全局.php

<?php
echo PHP_EOL . "Global.php executed." . PHP_EOL;
return array(
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=zf2tutorial;host=album.techmate.com',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter'
                    => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);
?>

testconfig.php.dist

<?php

echo PHP_EOL . "TestConfig.php.dist executed." . PHP_EOL;
return array(
    'modules' => array(
        'Album', // <-- Add this line
    ),
    'module_listener_options' => array(
        'config_glob_paths' => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);
?>

应用程序.config.php

<?php
echo PHP_EOL . "Application.config.php executed." . PHP_EOL;
return array(
    'modules' => array(
        'Album',                  // <-- Add this line
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);
?>

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

}

?>

而错误...

TestConfig.php.dist executed.


Module.config.php executed.
PHPUnit 3.7.10 by Sebastian Bergmann.

Configuration read from D:\PHP\zf2-tutorial\module\Album\test\phpunit.xml.dist

......E
SM AlbumTable executed.

SM AlbumTableGateway executed.


Time: 0 seconds, Memory: 8.50Mb

There was 1 error:

1) AlbumTest\Model\AlbumTableTest::testGetAlbumTableReturnsAnInstanceOfAlbumTable
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Zend\Db\Adapter\Adapter

D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:450
D:\PHP\zf2-tutorial\module\Album\Module.php:50
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:726
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:843
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:487
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:442
D:\PHP\zf2-tutorial\module\Album\Module.php:44
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:726
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:843
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:487
D:\PHP\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:442
D:\PHP\zf2-tutorial\module\Album\src\Album\Controller\AlbumController.php:33
D:\PHP\zf2-tutorial\module\Album\test\AlbumTest\Model\AlbumTableTest.php:149

FAILURES!
Tests: 7, Assertions: 9, Errors: 1.
4

3 回答 3

2

根据PHPUnit bootstrapping instructionsTestConfig.php.dist ,以下行看起来可能是错误的:

'config/autoload/{,*.}{global,local}.php',

将行更改为:

'../../../config/autoload/{,*.}{global,local}.php',

如果您有一个local.php配置文件,它也可能不会加载。

当然,如果目录结构不标准,相应地更改行,但你明白了。

于 2013-01-09T15:22:04.677 回答
1

我相信 global.php 没有被自动加载,因为文件的命名不匹配:

'config/autoload/{,*.}{global,local}.php',

因此,如果您命名文件myconfig.global.php,则应将其拾取。

于 2013-01-08T09:41:32.850 回答
1

我找到了解决这个问题的方法,但我对需要这样做感到非常失望。阅读 ZF2 手册,我发现 global.php 和 local.php 应该由 ModuleManager 与 module.config.php 合并。据我所知,这实际上应该发生在 ModuleManagerFactory 但是......

无论如何,我手动将 global.php 和 local.php 信息添加到 module.config.php 中。这行得通,但同样,它不应该是必要的。

与往常一样,我很高兴收到比这更好的答案。

为了清楚起见,我包括了手册参考。它来自第 700 页。请注意,它还包括调用文件 my.global.config.php 和 my.local.config.php 的错误。为了以防万一,我实际上确实尝试了这些名称,但这没有用。

154.5 Default Configuration Options
The following options are available when using the default services configured by the ServiceManagerConfig
and ViewManager.
These configuration directives can go to the config/autoload/{,*.}{global,local}.php files, or in the
module/<module name>/config/module.config.php configuration files. The merging of these configuration
files is done by the ModuleManager. It first merges each module’s module.config.php file, and then
the files in config/autoload (first the *.global.php and then the *.local.php files). The order of the
merge is relevant so you can override a module’s configuration with your application configuration. If you have both
a config/autoload/my.global.config.php and config/autoload/my.local.config.php, the
local configuration file overrides the global configuration.
于 2013-01-08T14:10:34.803 回答