1

我希望有人可以帮助我。

我正在阅读Rob Allen 的 Zend Framework 2 Tutorial

当我尝试去:

http://localhost/album/index

我收到以下错误:

[03-Sep-2012 17:48:07 UTC] PHP Fatal error:  Class 'Album\AlbumTable' not found in C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\module\Album\Module.php on line 33
[03-Sep-2012 17:48:07 UTC] PHP Stack trace:
[03-Sep-2012 17:48:07 UTC] PHP   1. {main}() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\public\index.php:0
[03-Sep-2012 17:48:07 UTC] PHP   2. Zend\Mvc\Application->run() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\public\index.php:12
[03-Sep-2012 17:48:07 UTC] PHP   3. Zend\EventManager\EventManager->trigger() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php:298
[03-Sep-2012 17:48:07 UTC] PHP   4. Zend\EventManager\EventManager->triggerListeners() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:209
[03-Sep-2012 17:48:07 UTC] PHP   5. call_user_func() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:469
[03-Sep-2012 17:48:07 UTC] PHP   6. Zend\Mvc\DispatchListener->onDispatch() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:469
[03-Sep-2012 17:48:07 UTC] PHP   7. Zend\Mvc\Controller\AbstractController->dispatch() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php:114
[03-Sep-2012 17:48:07 UTC] PHP   8. Zend\EventManager\EventManager->trigger() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php:108
[03-Sep-2012 17:48:07 UTC] PHP   9. Zend\EventManager\EventManager->triggerListeners() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:209
[03-Sep-2012 17:48:07 UTC] PHP  10. call_user_func() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:469
[03-Sep-2012 17:48:07 UTC] PHP  11. Zend\Mvc\Controller\AbstractActionController->onDispatch() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:469
[03-Sep-2012 17:48:07 UTC] PHP  12. Album\Controller\AlbumController->indexAction() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php:87
[03-Sep-2012 17:48:07 UTC] PHP  13. Album\Controller\AlbumController->getAlbumTable() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\module\Album\src\Album\Controller\AlbumController.php:26
[03-Sep-2012 17:48:07 UTC] PHP  14. Zend\ServiceManager\ServiceManager->get() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\module\Album\src\Album\Controller\AlbumController.php:18
[03-Sep-2012 17:48:07 UTC] PHP  15. Zend\ServiceManager\ServiceManager->create() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:412
[03-Sep-2012 17:48:07 UTC] PHP  16. Zend\ServiceManager\ServiceManager->createFromFactory() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:455
[03-Sep-2012 17:48:07 UTC] PHP  17. Zend\ServiceManager\ServiceManager->createServiceViaCallback() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:801
[03-Sep-2012 17:48:07 UTC] PHP  18. call_user_func() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:684
[03-Sep-2012 17:48:07 UTC] PHP  19. Album\Module->Album\{closure}() C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-tutorial\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:684

请注意,即使 AlbumTable.php 在 Album\Model 中,也找不到 Class 'Album\AlbumTable' 。

模块\相册\Module.php:

<?php

// module/Album/Module.php
namespace Album;

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

模块\专辑\src\专辑\模型\AlbumTable.php:

<?php

// module/Album/src/Album/Model/AlbumTable.php:
namespace Album\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;

class AlbumTable extends AbstractTableGateway
{
    protected $table ='album';

    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Album());
        $this->initialize();
    }

    public function fetchAll()
    {
        $resultSet = $this->select();
        return $resultSet;
    }

    public function getAlbum($id)
    {
        $id  = (int) $id;
        $rowset = $this->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->insert($data);
        } else {
            if ($this->getAlbum($id)) {
                $this->update($data, array('id' => $id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

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

模块\Album\src\Album\Controller\AlbumController.php:

<?php

// module/Album/src/Album/Controller/AlbumController.php:
namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AlbumController extends AbstractActionController
{

    protected $albumTable;

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

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

    public function addAction()
    {
    }

    public function editAction()
    {
    }

    public function deleteAction()
    {
    }
}

我的配置:

  • Windows 7的。
  • Zend Server CE 技术预览版 (PHP 5.4) (注意我尝试使用 Zend Server CE (PHP 5.3) 但得到同样的错误)。
  • Apache 和 MySQL 在其他 PHP 页面上工作得很好,例如,我可以在一个简单的 PHP 脚本中使用 PDO 连接到“专辑”表。

你们中有人知道问题可能是什么吗?

4

2 回答 2

4

类是Album\Model\AlbumTable,但在错误消息中它是Album\AlbumTable

堆栈跟踪将您引导至 ServiceManager。Album\Model\AlbumTables添加到 ServiceManager 的闭包Album\Module::getServiceConfig()

在那里你可以看到$table = new AlbumTable($dbAdapter);命名空间Album解析为Album\AlbumTable.
这很容易解决:use Album\Model\AlbumTable;在命名空间声明后添加到 module\Album\Module.php

于 2012-09-04T04:30:20.963 回答
1

将您的行替换为

$table = new \Album\Model\AblumTable($dbAdapter);

或者在类之前在文件顶部添加一个使用。

use Album\Model\AlbumTable;
于 2012-10-14T00:25:22.323 回答