phpzend-framework

Zend skeleton application Class 'Album\Model\AlbumTable' not found


I'm trying to figure out what's wrong with my first tutorial using Zend Skeleton App. I'm using Zend Studio 10 + ZendServer and Zf2.2; managed to get the skeleton app working and now got stuck on a missing class problem (see error below). I have tried various approaches but the result is the same: it's not working. Here are my files, any help would be appreciated.

My error:

Fatal error: Class 'Album\Model\AlbumTable' not found in C:\Program Files\Zend\Apache2\htdocs\zf2album\module\Album\Module.php on line 55

Album/Module.php

namespace Album;

use Album\Model\Album;

use Album\Model\AlbumTable;

use Zend\Db\TableGateway\TableGateway;

use Zend\ModuleManager\Feature\ServiceProviderInterface;

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

the AlbumController.php

namespace Album\Controller;

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

class AlbumController extends 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;
} }

AlbumModel.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('Form id does not exist');
        }
    }
}

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

Solution

  • Assuming this isn't a typo in your question, the filename for the class AlbumTable should be AlbumTable.php, not AlbumModel.php.