2013-12-08 104 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

相關問題