Zend_Db_Table :: getDefaultAdapter不起作用

时间:2011-07-13 06:34:57

标签: zend-framework php

嗨我正在使用zend框架,在我的登录控制器索引操作中。当用户提交登录表单时,我试图获取DefaultAdapter,我尝试了

$db = Zend_Db_Table::getDefaultAdapter();

这不起作用。

但我用了

        $db = Zend_Db::factory('Pdo_Mysql', array(
                                                'host'     => 'localhost',
                                                'username' => 'root',
                                                'password' => '123',
                                                'dbname'   => 'test'
                                            ));

然后它正在运作。

这是我的索引动作

public function indexAction() {

    $loginform = new Application_Form_LoginForm();

    if($this->getRequest()->getPost()) {

        $filter = new Zend_Filter_StripTags();

        $name = $filter->filter($this->getRequest()->getPost('name'));
        $password = $filter->filter($this->getRequest()->getPost('password'));          

        //$db = Zend_Db_Table::getDefaultAdapter();

        $db = Zend_Db::factory('Pdo_Mysql', array(
                                                'host'     => 'localhost',
                                                'username' => 'root',
                                                'password' => '123',
                                                'dbname'   => 'test'
                                            ));

        $authAdapter = new Zend_Auth_Adapter_DbTable($db);

        $authAdapter->setTableName('users');
        $authAdapter->setIdentityColumn('username');
        $authAdapter->setCredentialColumn('password');

        // Set the input credential values to authenticate against
        $authAdapter->setIdentity($name);
        $authAdapter->setCredential($password);

        // do the authentication
        $auth = Zend_Auth::getInstance();
        $result = $auth->authenticate($authAdapter);

        if ($result->isValid()) {
            // success : store database row to auth's storage system except the password column
            $data = $authAdapter->getResultRowObject(null, 'password');
            $auth->getStorage()->write($data);
            $this->_redirect('/index/');
        } else {
            $this->view->errors = array(0 => array(0 => 'Username and/or password are invalid.'));
            $this->view->form = $loginform;
        }

    }
    else {

        $this->view->form = $loginform;
    }   
}

我在我的application.ini中定义了我的数据库连接详细信息,如下所示

resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "123"
resources.db.params.dbname = "test"

为什么我的Zend_Db_Table::getDefaultAdapter();无效,错误是

An error occurred
Application error
Exception information:

Message: No database adapter present
Stack trace:

#0 /home/kanishka/workspace/hospital_system/library/Zend/Auth/Adapter/DbTable.php(140): Zend_Auth_Adapter_DbTable->_setDbAdapter(NULL)
#1 /home/kanishka/workspace/hospital_system/application/controllers/LoginController.php(30): Zend_Auth_Adapter_DbTable->__construct(NULL)
#2 /home/kanishka/workspace/hospital_system/library/Zend/Controller/Action.php(513): LoginController->indexAction()
#3 /home/kanishka/workspace/hospital_system/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('indexAction')
#4 /home/kanishka/workspace/hospital_system/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#5 /home/kanishka/workspace/hospital_system/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#6 /home/kanishka/workspace/hospital_system/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#7 /home/kanishka/workspace/hospital_system/public/index.php(26): Zend_Application->run()
#8 {main}  

Request Parameters:

array (
  'controller' => 'login',
  'action' => 'index',
  'module' => 'default',
  'name' => '',
  'password' => '',
  'submit' => 'submit',
)  

3 个答案:

答案 0 :(得分:1)

将此添加到您的配置

resources.db.isDefaultTableAdapter = true

答案 1 :(得分:0)

您应该将此功能添加到Bootstrap

protected function _initDatabase(){
        $db = $this->getPluginResource('db')->getDbAdapter();
        Zend_Db_Table::setDefaultAdapter($db); //important
        Zend_Registry::set('db', $db);    
}

答案 2 :(得分:0)

尝试添加此

SetEnv APPLICATION_ENV development

在文件PROJECTNAME \ public \ .htaccess

的末尾