Construct()必须是一个实例


Construct() must be an instance

我正在遵循ZF2手册,并面临以下错误:

"可捕获的致命错误:传递到Album''Model''AlbumTable::__construct()的参数1必须是Zend''Db''TableGateway的实例,给定的Zend''Db ''Adapter''Adapter的实例,在第33行的/var/www/CommunicationApp/module/Album/module.php中调用,并在第11行的/var/www/CommunicationApp/module/Album/src/Album/DeModel/AlbumTable.php中定义"

我不知道我遗漏了什么,因为它和教程完全一样。

<?php
namespace Album'Model;
use Zend'Db'TableGateway'TableGateway;
class AlbumTable
{
 protected $tableGateway;
 public function __construct(TableGateway $tableGateway)
 {
     $this->tableGateway = $tableGateway;
 }
 public function fetchAll()
 {
     $resultSet = $this->tableGateway->select();
     return $resultSet;
 }
 public function getAlbum($id)
 {
     $id  = (int) $id;
     $rowset = $this->tableGateway->select(array('id' => $id));
     $row = $rowset->current();
     if (!$row) {
         throw new 'Exception("Could not find row $id");
     }
     return $row;
 }
 public function saveAlbum(Album $album)
 {
     $data = array(
         'artist' => $album->artist,
         'title'  => $album->title,
     );
     $id = (int) $album->id;
     if ($id == 0) {
         $this->tableGateway->insert($data);
     } else {
         if ($this->getAlbum($id)) {
             $this->tableGateway->update($data, array('id' => $id));
         } else {
             throw new 'Exception('Album id does not exist');
         }
     }
 }
 public function deleteAlbum($id)
 {
     $this->tableGateway->delete(array('id' => (int) $id));
 }
 }

Module.php:

<?php
namespace Album;
use Album'Model'AlbumTable;

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) {
                $dbAdapter = $sm->get('Zend'Db'Adapter'Adapter');
                $table     = new AlbumTable($dbAdapter);
                return $table;
            },
        ),
    );
}
}

您应该将TableGetway传递给AlbumTable。更改Module.php并将getServiceConfig替换为:

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