2011-07-13 53 views
1

您好我正在使用zend框架,在我的登錄控制器索引操作。當用戶提交登錄表單,我試圖讓DefaultAdapter,我嘗試了通過Zend_Db_Table :: getDefaultAdapter不起作用

$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', 
) 
+0

的末尾添加此

SetEnv APPLICATION_ENV development 

你有一個' _initDb'方法在你的Bootstrap中? –

回答

1

將此添加到您的配置

resources.db.isDefaultTableAdapter = true 
+0

我試過了,但還是一樣。 :( –

+0

@littletipz除非你重寫了Db資源,否則默認適配器將被註冊。 – Xerkus

+0

mm我想我沒有重寫Db資源...... :( –

0

您應該添加此功能爲引導

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

嘗試在文件PROJECTNAME \ PUBLIC \的.htaccess