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 に答える