2013-12-08 83 views
0

我正在关注的ZF2 Manual,我面临着这样的错误:构建()必须是一个实例

“开捕致命错误:传递给相册\型号\ AlbumTable参数1 :: __构造()必须是Zend \ Db \ TableGateway \ TableGateway实例,Zend \ Db \ Adapter \ Adapter实例,在第33行调用/var/www/CommunicationApp/module/Album/Module.php并在/ var/www/CommunicationApp /模块/专辑/ src /专辑/型号/ AlbumTable.php上线11“

我不知道我失踪,因为它是完全一样的教程。

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

回答

3

你应该通过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); 
      }, 
     ), 
    ); 
} 
+0

谢谢你, – John

相关问题