2012-07-06 47 views
4

这是Rob Allen的Zend Framework beta4快速入门教程。如何获取Zend Framework 2 beta4中的服务管理器来创建专辑表的实例?

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

好像它没有试图使该数据库的连接,但我还没有找到告诉的方式。它使用闭包从ServiceManager返回一个实例,但获取上述错误消息。

module/Album/Module.php

namespace Album;

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

namespace Application;

使用Zend的\ DB \适配器\适配器作为将对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\'' 
     ), 
    ), 
); 
+0

有同样的问题... – 2012-07-06 11:10:04

+0

有同样的问题也 – Developer 2012-07-24 12:28:47

+0

嗨w2wDev做你解决了这个问题,如果是的话,是有帮助的,如果你可以分享您的解决方案 – 125369 2012-07-26 10:56:33

回答

2

它相关的事实是,由于Beta 4的Zend框架的主人已经改变所以我的测试版4针对教程不再适用于最新的ZF主。

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

更新
截至2012年7月11日,我的tutorial现在更新的测试版5,它现在使用数据库适配器的服务工厂,以创建适配器,所以你甚至不需要修改应用程序的模块级的更多。

+2

显然的问题是在$将对DBAdapter = $ sm-> get('db-adapter');行 – 2012-07-06 11:43:37

0

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

感谢阿尔芒,但我确实有,我现在得到︰ – w2wDev 2012-07-06 14:36:07

+0

感谢阿尔芒,但我已经在那里。 – w2wDev 2012-07-06 14:40:15

+0

@Armand我得到相同的确切的错误,什么w2wDev发布。有什么建议么。我的主要Module.php有一个引用你提到的getServiceConfiguration(),但仍然得到相同的错误。任何帮助,将不胜感激 – 125369 2012-07-25 14:34:12

0

更新您的composer.json文件。

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

运行下面的命令,你会很好去。

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

我添加了提供给module.php的代码,它没有执行。我将密钥更改为Zend \ db \ Adapter \ Adapter并导致它执行。但是,我收到了错误未定义的索引:db $ line = $ config ['db'];因为$ config不包含该密钥。

对我来说,显然有一些额外的代码需要将数据库密钥加载到$ 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); 
       }, 
      ), 
     ); 
    } 

} 

?> 
0

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

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