我是zend框架的新手。 我在模型文件夹中创建了Users.php。 模型文件夹的代码如下。
<?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();
}
}
?>
我创建了一个表单,然后从控制器传递从表单到模型的数据,然后模型将数据插入到数据库中。
但是,如果我尝试创建用户类的对象,则会产生致命错误。
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)
如果我尝试创建一个Application_Model_DbTable_User类,它工作正常,当我调用该类的方法时,它也可以正常工作,但是当我想将数据插入数据库时,它会生成致命错误如上所述。
这是我的控制器代码。
$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);
这是确切的错误:
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}
答案 0 :(得分:1)
我认为您已删除ErrorController.php
或未在默认模块中首先创建它。现在讨厌的是一些应用程序错误正在发生,因此ZF正在将控制器转移到ErrorController.php
,但由于它不存在,你会遇到这样的致命错误。
错误控制器应该包含
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;
}
}
更新
你不能创建直接扩展Zend_Db_Table_Row_Abstract的类的实例,而应该使用table对象来实现 例如
$user = new Users();
$user->createRow()->insert_user($data);
而不是
$obj = new Application_Model_DbTable_User();
$obj->insert_user($data);