2011-07-13 15 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' 
              )); 

を使用。

これは私のindexアクション

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; 
    } 
} 

である私は私のZend_Db_Table::getDefaultAdapter();が機能しない理由、エラーが

あるこの

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" 

ように私はapplication.iniで私のデータベース接続の詳細を定義しています

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

\ファイルPROJECTNAMEの最後でこの

SetEnv APPLICATION_ENV development 

を追加しよう –

答えて

1

設定に追加

resources.db.isDefaultTableAdapter = true 
+0

私はそれを試しましたが、それでも同じです。 : –

+0

@littletipz Dbリソースをオーバーライドしないと、デフォルトのアダプタが登録されます – Xerkus

+0

mm私はDbリソースをオーバーライドしていないと思います......: –

0

あなたは、公共の\ .htaccessの

0

protected function _initDatabase(){ 
     $db = $this->getPluginResource('db')->getDbAdapter(); 
     Zend_Db_Table::setDefaultAdapter($db); //important 
     Zend_Registry::set('db', $db);  
} 
ブートストラップ

にこの機能を追加する必要がありますが、 `持っていますか_initDb`メソッドをブートストラップに追加しますか?
関連する問題