1

我是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}
4

1 回答 1

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 的类的实例,而是应该使用表对象来这样做,例如

$user = new  Users();
$user->createRow()->insert_user($data);

代替

$obj = new Application_Model_DbTable_User();
            $obj->insert_user($data);
于 2012-05-28T09:39:15.587 回答