user1644380
user1644380

Reputation:

Rob Allen's Zend Framework 2 Tutorial: PHP Fatal error: Class 'Album\AlbumTable' not found

I hope someone can help me with this.

I am working my way through Rob Allen's Zend Framework 2 Tutorial.

When I try to go to:

http://localhost/album/index

I get the following error:

[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

Note it is saying Class 'Album\AlbumTable' not found even though AlbumTable.php is in Album\Model.

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

module\Album\src\Album\Model\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));
    }
}

module\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()
    {
    }
}

My configuration:

Do any of you have any idea what the problem might be?

Upvotes: 1

Views: 13159

Answers (2)

Kdecom
Kdecom

Reputation: 728

Replace your line with

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

Or add a use on top of file before the class.

use Album\Model\AlbumTable;

Upvotes: 1

Xerkus
Xerkus

Reputation: 2705

Class is Album\Model\AlbumTable but in error message it is Album\AlbumTable

Stack trace leading you to ServiceManager. Closure for Album\Model\AlbumTables added to ServiceManager in Album\Module::getServiceConfig()

There you can see $table = new AlbumTable($dbAdapter); while namespace is Album it resolves to Album\AlbumTable.
This is easy to fix: add use Album\Model\AlbumTable; to module\Album\Module.php after namespace declaration

Upvotes: 4

Related Questions