Zend \ ServiceManager \ ServiceManager :: get无法为Zend \ Db \ Adapter \ Adapter获取或创建实例

时间:2013-04-15 12:41:32

标签: php zend-framework2

更新

虽然尝试如此提及此问题已被回答我收到此错误

Fatal error: Class Album\Module contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Zend\ModuleManager\Feature\ServiceProviderInterface::getServiceConfig) in D:\projects\zf2-tutorial\module\Album\Module.php on line 54

我已经关注了ZF2 Documentation,当时间到来显示列表我有这个错误。我搜索了很多,但无法找到解决方法:(

在我的config / application.config.php中,首先我有了这个

'config_glob_paths' => array(
      'config/autoload/{,*.}{global,local}.php',
    )

,错误是

D:\projects\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:744

消息:

An exception was raised while creating "Album\Model\AlbumTable"; no instance returned

搜索后我得到了这个,所以我将上面的代码更改为

    'config_glob_paths' => array(
        '../../../config/autoload/{,*.}{global,local}.php',
    )

现在显示此错误

D:\projects\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:456

消息:

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Zend\Db\Adapter\Adapter

我还将local.php.dist更改为local.php,但仍然没有运气:(

这是我的Album / 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;


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

我的相册/型号/ AlbumTable.php

<?php
 namespace Album\Model;

use Zend\Db\TableGateway\TableGateway;

class AlbumTable{
  protected $_albumObj;

   public function __construct(TableGateway $_albumObj){
      $this->_albumObj = $_albumObj;
   }

   public function fetchAll(){
      $res = $this->_albumObj->select();
      return $res;
   }

public function getAlbum($id){
    $row = $this->_albumObj->select(array('id'=>(int)$id));
    $row = $row->current();
    if($row){
        return $row;
    }
}

public function deleteAlbum($id){
    return $this->_albumObj->delete(array('id'=>(int)$id));
}


public function saveAlbum(Album $album){
    $data = array(
        'name' => $album->name,
        'author'  => $album->author,
    );

    $id = (int)$album->id;
    if ($id == 0) {
        $this->_albumObj->insert($data);
    } else {
        if ($this->getAlbum($id)) {
            $this->_albumObj->update($data, array('id' => $id));
        } else {
            throw new \Exception('Form id does not exist');
        }
    }
  }
 }

&GT;

我的AlbumController.php

<?php
 namespace Album\Controller;

 use Zend\Mvc\Controller\AbstractActionController;
 use Zend\View\Model\ViewModel;

class AlbumController extends AbstractActionController {
   protected $albumTable;

  public function indexAction(){
    //$re = $this->getAlbumTable()->fetchAll();
    //print_r($re);
    return new ViewModel(array('albums' => $this->getAlbumTable()->fetchAll()));
}

public function addAction(){}
public function editAction(){}
public function deleteAction(){}
public function getAlbumTable(){
    if (!$this->albumTable) {
        $sm = $this->getServiceLocator();
        $this->albumTable = $sm->get('Album\Model\AlbumTable');
        //var_dump($this->albumTable);
    }
    return $this->albumTable;
 }
}

我的golbal.php

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

我卡住了不能继续:(任何帮助将不胜感激!

更新

在我的问题的顶部尝试如此提到我收到此错误

Fatal error: Class Album\Module contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Zend\ModuleManager\Feature\ServiceProviderInterface::getServiceConfig) in D:\projects\zf2-tutorial\module\Album\Module.php on line 54

0 个答案:

没有答案