1
votes

I already saw a few questions in this direction but none that actually helped in my case.

PHP Fatal error: Uncaught Error: Class 'Booklist\Model\BookTable' not found in /var/www/html/module/BookList/Module.php:40\

This is the error I extracted from my /var/log/apache2/error.log.

I already looked for people with the same questions but I could not find an solution. I already included the file and the Namespace but I apparently cant access the file.

I'm working with zenframework 2.2.10.

This is how my file Structure looks:

module 
-Application
-BookList
   -config
      \module.config.php
   -src
     -BookList
        -Controller
           \BookController.php
        -Form
           \BookForm.php
        -Model
           \Book.php
           \BookTable.php
   -view
      -book-list
         -book
            \..
   \autoload_classmap.php
   \module.php

First this is my Module.php:

namespace BookList;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

use Booklist\Model\BookTable;
use BookList\Model\Book;

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(
                'BookList\Model\BookTable' => function($sm){
                    $tableGateway = $sm->get('BookTableGateway');
                    $table = new BookTable($tableGateway);

                    return $table;
                },
                'BookTableGateway' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new 
Book());
                    return new 
TableGateway('book',$dbAdapter,null,$resultSetPrototype);
                },
            ),
        );
    }
}

This is my BookTable.php:

namespace BookList\Model;

use Zend\Db\TableGateway\TableGateway;

class BookTable {
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }
    public function fetchAll(){
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }
    public function getBook($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 saveBook(Book $book){
        $data = array(
            'title' => $book->title,
            'author' => $book->author,
        );

        $id = (int) $book->id;
        if($id == 0){
            $this->tableGateway->insert($data);
        }else{
            if($this->getBook($id)){
                $this->tableGateway->update($data,array('id'=>$id));
            }
            else{
                throw new \Exception("Book id does not exist");
            }
        }
    }
    public function deleteBook($id){
        $this->tableGateway->delete(array('id'=>(int) $id));
    }

}

And finally, this is my BookController.php:

namespace BookList\Controller;
use BookList\Model\Book;

use BookList\Form\BookForm;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class BookController extends AbstractActionController{
    protected $bookTable;

    public function indexAction()
    {
        return new ViewModel(array(
            'books' => $this->getBookTable()->fetchAll(),
        ));
    }
    public function addAction()
    {
        $form = new BookForm();
        $form->get('submit')->setValue('Add');

        $request = $this->getRequest();
        if($request->isPost()){

        }
        return array('form'=>$form);
    }

    public function editAction()
    {
        $form = new BookForm();
        //$book->bind($book);
        $form->get('submit')->setAttribute('value','Edit');
        $request = $this->getRequest();
        if($request->isPost()){

        }
        return array(
            'id' => $id,
            'form' => $form,
        );
    }

    public function deleteAction()
    {
        $id = (int) $this->params()->fromRoute('id',0);
        if(!$id){
            return $this->redirect()->toRoute('book');
        }

        $request = $this->getRequest();
        if($request -> isPost()){

        }
        return array(
            'id' => $id,
            //'book' => ,
        );
    }
    public function getBookTable(){
        if(!$this->bookTable){
            $sm = $this->getServiceLocator();
            $this->bookTable = $sm->get('BookList\Model\BookTable');
        }
        return $this->bookTable;
    }
}

The error causes when I am trying to call this class:

$table = new BookTable($tableGateway);

Maybe anyone here got an idea what I could do.

1

1 Answers

1
votes

In Module.php:

use Booklist\Model\BookTable;

should be:

use BookList\Model\BookTable;

(note the capital 'L').