问题
I am new to zend framework. I have created Users.php in the model folder. The code of the model folder is as following.
<?php
include("Zend/Db/Table/Abstract.php");
include("Zend/Db/Table/Row/Abstract.php");
class users extends Zend_Db_Table_Abstract {
protected $_name = 'users';
protected $_rowClass = 'Application_Model_DbTable_User';
public function fetchUsers() {
$select = $this->select();
//$select->where('completed = 0');
$select->order(array('date_created DESC', 'user_id ASC'));
return $this->fetchAll($select);
}
public function insert_user1(array $data) {
echo "called";
}
}
class Application_Model_DbTable_User extends Zend_Db_Table_Row_Abstract {
public function insert_user(array $data) {
print_r($data);
//$obj = new users();
}
}
?>
I have created a form and from the controller I am passing the data getting from the form to the model so that the model then inserts that data in database.
But if I try to create object of the users class It generated an fatal error.
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)
And if If I try to create an Application_Model_DbTable_User class, It works fine and When I call the method of that class, It also works fine but then when I want to insert the data to the database it then generates the fatal error as mentioned above.
Here is my controller code.
$data = array('first_name' => $request->getParam('first_name'),
'last_name' => $request->getParam('last_name'),
'country' => $request->getParam('country'),
'address' => $request->getParam('address'),
'username' => $request->getParam('user_name'),
'password' => $request->getParam('password'),
'password' => $request->getParam('address'),
'date_created' => date('Y-m-d H:i:s')
);
//$tasksGateway = new users();
$obj = new Application_Model_DbTable_User();
$obj->insert_user($data);
Here is the exact error:
exception 'Zend_Db_Table_Exception' with message 'No adapter found for users' in C:\wamp\www\alancer\library\Zend\Db\Table\Abstract.php:755 Stack trace: #0 C:\wamp\www\alancer\library\Zend\Db\Table\Abstract.php(739): Zend_Db_Table_Abstract->_setupDatabaseAdapter() #1 C:\wamp\www\alancer\library\Zend\Db\Table\Abstract.php(268): Zend_Db_Table_Abstract->_setup() #2 C:\wamp\www\alancer\application\models\DBTable\Users.php(22): Zend_Db_Table_Abstract->__construct() #3 C:\wamp\www\alancer\application\controllers\UserController.php(55): Application_Model_DbTable_User->insert_user(Array) #4 C:\wamp\www\alancer\library\Zend\Controller\Action.php(516): UserController->processAction() #5 C:\wamp\www\alancer\library\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('processAction') #6 C:\wamp\www\alancer\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #7 C:\wamp\www\alancer\library\Zend\Controller\Front.php(212): Zend_Controller_Front->dispatch() #8 C:\wamp\www\alancer\web_root\index.php(35): Zend_Controller_Front::run('../application/...') #9 {main}
回答1:
I think you have deleted ErrorController.php or not have created it in first place inside your default module . Now what is happing is some application error is occuring hence ZF is shifting controller to ErrorController.php but since it do not exist you are getting such fatal error.
Error Controller should contain
class ErrorController extends Zend_Controller_Action
{
public function errorAction()
{
$errors = $this->_getParam('error_handler');
if (!$errors || !$errors instanceof ArrayObject) {
$this->view->message = 'You have reached the error page';
return;
}
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setHttpResponseCode(404);
$this->view->message = 'Page not found';
break;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$this->view->message = 'Application error';
break;
}
// conditionally display exceptions
if ($this->getInvokeArg('displayExceptions') == true) {
$this->view->exception = $errors->exception;
}
$this->view->request = $errors->request;
}
}
Update
You cannot create instance of classes which extends Zend_Db_Table_Row_Abstract directly instead you should use table object to do so for e.g
$user = new Users();
$user->createRow()->insert_user($data);
Instead of
$obj = new Application_Model_DbTable_User();
$obj->insert_user($data);
来源:https://stackoverflow.com/questions/10781753/fatal-error-uncaught-exception-zend-controller-dispatcher-exception-while-cre