您好我正在使用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',
)
的末尾添加此
你有一個' _initDb'方法在你的Bootstrap中? –