2017-01-03 37 views
0

爲什麼我得到這個錯誤,即使我已經添加了所有的類?zf2 - 無法將Zend Db Adapter Adapter服務解析到工廠

無法將服務「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, 
    ], 

]; 
+0

這可能是與你的application.config.php一個問題...你可以張貼的內容?你使用的是哪個版本的骨架應用程序? –

+0

@PurpleHexagon - 我已經更新了我的application.config.php文件..請檢查 –

+0

您是否將以下答案中的數據庫配置代碼添加到其中一個自動加載的配置文件中?如果是這樣的話? –

回答

0

Probaly因爲$sm->get('Zend\Db\Adapter\Adapter')是不是一種服務,但一個班級設立Adapter,但尚未在ServiceManager內登記。

通過向其中注入Config來創建一個爲您建造Adapter課程的工廠。然後在您的serviceConfig中註冊此AdapterFactory,它會返回您的適配器。或者,您可以使用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

1

的文檔,你可能已經錯過了你是以下教程的一個步驟。

在配置/自動加載/ 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', 
     ), 
    ), 
); 
相關問題