0

我试图弄清楚我使用 Zend Skeleton App 的第一个教程有什么问题。我正在使用 Zend Studio 10 + ZendServer 和 Zf2.2;设法使骨架应用程序正常工作,但现在陷入了缺少班级的问题(请参阅下面的错误)。我尝试了各种方法,但结果是一样的:它不起作用。这是我的文件,任何帮助将不胜感激。

我的错误:

致命错误:第 55 行的 C:\Program Files\Zend\Apache2\htdocs\zf2album\module\Album\Module.php 中找不到类 'Album\Model\AlbumTable'

专辑/模块.php

命名空间专辑;

使用专辑\型号\专辑;

使用专辑\型号\专辑表;

使用 Zend\Db\TableGateway\TableGateway;

使用 Zend\ModuleManager\Feature\ServiceProviderInterface;

类模块实现 ServiceProviderInterface {

public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php',
        ),
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
      // if we're in a namespace deeper than one level we need to fix the \ in the path
                __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
            ),
        ),
    );
}

public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}



// Add this method:
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);
                  },
          ),
  );
}
 }

AlbumController.php

命名空间专辑\控制器;

使用 Zend\Mvc\Controller\AbstractActionController;使用 Zend\View\Model\ViewModel;

类 AlbumController 扩展 AbstractActionController { protected $albumTable;

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

public function addAction()
{
}

public function editAction()
{
}

public function deleteAction()
{
}

public function fooAction()
{
    // This shows the :controller and :action parameters in default route
    // are working when you browse to /album/album/foo
    return array();
}

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

专辑模型.php

命名空间专辑\型号;

使用 Zend\Db\TableGateway\TableGateway;

类 AlbumTable { 受保护 $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('Form id does not exist');
        }
    }
}

public function deleteAlbum($id)
{
    $this->tableGateway->delete(array('id' => $id));
} }
4

1 回答 1

1

假设这不是您的问题中的拼写错误,则该类的文件名AlbumTable应该是AlbumTable.php,而不是AlbumModel.php.

于 2013-05-23T14:45:25.163 回答