开发者

Zend_Db_Table::getDefaultAdapter is not working

开发者 https://www.devze.com 2023-03-20 02:50 出处:网络
hi i am using zend framework , in my login controller index action . when a user submit the login form , i am trying to get the DefaultAdapter, i tried it by

hi i am using zend framework , in my login controller index action . when a user submit the login form , i am trying to get the DefaultAdapter , i tried it by

$db = Zend_Db_Table::getDefaultAdapter();

and this is not working .

but i used

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

then it is working .

this is my index action

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');
        $authAd开发者_如何学Capter->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;
    }   
}

i have define my database connection details in my application.ini like this

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"

why my Zend_Db_Table::getDefaultAdapter(); is not working , the error is

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',
)  


Add this to your configuration

resources.db.isDefaultTableAdapter = true


You should add this function into Bootstrap

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


Try to add this

SetEnv APPLICATION_ENV development

in the end of file PROJECTNAME\public\.htaccess

0

精彩评论

暂无评论...
验证码 换一张
取 消