zf2 - 无法将服务Zend \ Db \ Adapter \ Adapter解析为工厂

时间:2017-01-03 12:33:31

标签: php zend-framework2

为什么即使我已经添加了所有课程,我也会收到此错误?

  

无法解析服务" Zend \ Db \ Adapter \ Adapter"到工厂;是   您确定在配置期间提供了它吗?

这是我的Module.php:

namespace Album;

use Album\Model\Album; 
use Album\Model\AlbumTable; 
use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway;

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 getServiceConfig()
    { 
       return array(
             'factories' => array(
                 'Album\Model\AlbumTable' =>  function($sm) {
                     $tableGateway = $sm->get('AlbumTableGateway');
                     $table = new AlbumTable($tableGateway);
                     return $table;
                 },
                 'AlbumTableGateway' => function ($sm) {
                     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                     $resultSetPrototype = new ResultSet();
                     $resultSetPrototype->setArrayObjectPrototype(new Album());
                     return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                 },
             ),
         );
    }   

    public function getConfig()
    { 
        return include __DIR__ . '/config/module.config.php';
    } 
}

更新: 文件 - application.config.php:

return [
    // Retrieve list of modules used in this application.
    'modules' => [
        'Zend\Router',
        'Zend\Validator',
        'Application',
        'Album',
        'Blog',
    ],

    // These are various options for the listeners attached to the ModuleManager
    'module_listener_options' => [
        'module_paths' => [
            './module',
            './vendor',
        ],
        'config_glob_paths' => [
            // realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php',
            realpath(__DIR__) . '/autoload/{,*.}{global,local}.php',
        ],


        'config_cache_enabled' => false,

        // The key used to create the configuration cache file name.
        'config_cache_key' => 'application.config.cache',


        'module_map_cache_enabled' => false,

        // The key used to create the class map cache file name.
        'module_map_cache_key' => 'application.module.cache',

        // The path in which to cache merged configuration.
        'cache_dir' => 'data/cache/',


        // 'check_dependencies' => true,
    ],

];

2 个答案:

答案 0 :(得分:1)

您可能错过了您所关注的教程中的一步。

在config / autoload / global.php中添加:

return array(
     'db' => array(
         'driver'         => 'Pdo',
         'dsn'            => 'mysql:dbname=zf2tutorial;host=localhost',
         'driver_options' => array(
             PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
         ),
     ),
     'service_manager' => array(
         'factories' => array(
             'Zend\Db\Adapter\Adapter'
                      => 'Zend\Db\Adapter\AdapterServiceFactory',
         ),
     ),
 );

答案 1 :(得分:0)

Probaly,因为$sm->get('Zend\Db\Adapter\Adapter')不是服务,而是设置Adapter的类,但尚未在ServiceManager中注册。

创建一个工厂,通过向其中注入Adapter来为您构建Config类。然后在AdapterFactory中注册返回适配器的serviceConfig。或者您可以使用Zend的默认AdapterFactory,它使用' db'在您的配置中设置数据库适配器的密钥。

在您的应用程序Module.php中注册适配器:

public function getServiceConfig()
{
    return [
        'factories' => [
            \Zend\Db\Adapter\Adapter::class => \Zend\Db\Adapter\AdapterServiceFactory::class,
        ]
        // rest of your configuration
    ];
}

请参阅ZF2 v2.4 - Setting up a database adapter

的文档