Richard Knop
Richard Knop

Reputation: 83697

Method "_getTable" does not exist and was not trapped in __call()

So here is a method I have in my controller:

private $_tables = array();

private function _getTable($table)
{
    if (!isset($this->_tables[$table])) {
        include APPLICATION_PATH . '/modules/'
        . $this->_request->getModuleName() . '/models/' . $table . '.php';
        $this->_tables[$table] = new $table();
    }
    return $this->_tables[$table];
}

I'm using it like this in controller actions:

public function pageAction()
{
    $request = $this->getRequest();
    $pages = $this->_getTable('Pages');

    $p = $pages->getSingle($request->getParam('id'));

    // increment number of views in the database
    // it only gets incremented when IP is not the same
    // as last viewer's IP
    $pages->viewIncrement($p->id);

    $this->view->headTitle($p->title
                           . ' - Some cool title');
    $this->view->p = $p;
}

It works great on localhost when testing on my PC. It also worked on my older web host but recently I have moved my ZF applications to a new web hosting provider and when viewing certain pages I get this error:

Method "_getTable" does not exist and was not trapped in __call()

That's all that's displayed, there is nothing else. Some pages work though and some show this error so I'm completely lost. What could be the problem?

All required PHP extensions are installed and configured correctly, database connection works fine (because some pages work ok). I really have no idea. If at least the error was more descriptive.

Upvotes: 3

Views: 3033

Answers (2)

user32117
user32117

Reputation:

Are you sure that the actions that doesn't work are extending your custom controller that implements this method? In this example:

class MyController extends Zend_Controller_Action
{
    protected functoin _getTable($table)
    {
        // implementation
    }
}


class ProductController extends MyController
{
     public function indexAction()
     {
         $this->_getTable('sometable'); // calls MyController::_getTable
     }
}

all your controllers are extending MyController?

As Nazariy have said, you need to set the visibility accessor of the method to protected or public. private accessor will not let you call this method from extending classes.

Edit: Since you said you´re implement the _getTable method into every controller, what are the chances that you have misspelled the method name on implementation? Maybe something like __getTable (double underscore) or _getTalbe (inverted letters)?

I know it´s a basic thing, but sometimes you´re too focused in the problem and don´t see the common reslution (it happens to me a lot).

Even if it´s not the problem, you should consider creating your base controller and extending from it, to avoid this code duplication.

Upvotes: 1

Nazariy
Nazariy

Reputation: 6088

Try to change private state to something less restrictive, like public.

Also make sure that all classes implement same controller.

Upvotes: 0

Related Questions