0

更新

在尝试提到这个问题已经得到回答时,我收到了这个错误

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 文档显示列表时遇到了这个错误。我用谷歌搜索了很多,但找不到修复它的方法:(

在我的 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 但仍然没有运气:(

这是我的专辑/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');
        }
    }
  }
 }

?>

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

我的gobal.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
4

0 回答 0