0

エラーが発生しました:Zend \ ServiceManager \ Exception \ ServiceNotCreatedException:「Zend \ Db \ Adapter\Adapter」の作成中に例外が発生しました。インスタンスは返されません。

これをたどると、Zend \ Db \ Adapter\Adapterがglobal.phpに表示されていることがわかりました。ただし、エラーが発生しているmodule.php getserviceconfig()の前に、global.phpが呼び出されることはないか、少なくとも呼び出されていません。testconfig.php.distとapplication.config.phpはどちらも、global.phpをリスナー定義に提示します。ただし、global.phpは実行されません。

誰かが理由を教えてもらえますか?定義は次のとおりです。

global.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',
        ),
    ),
);
?>

application.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のブートストラップ手順によるとTestConfig.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 に答える