【问题标题】:Zend_Db_Table::getDefaultAdapter is not workingZend_Db_Table::getDefaultAdapter 不工作
【发布时间】:2011-07-13 06:34:56
【问题描述】:

您好,我正在使用 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',
)  

【问题讨论】:

  • 你的 Bootstrap 中有 _initDb 方法吗?

标签: zend-framework php


【解决方案1】:

将此添加到您的配置中

resources.db.isDefaultTableAdapter = true

【讨论】:

  • @littletipz 除非您覆盖 Db 资源,否则将注册默认适配器。
  • @littletipz 在 Zend_Application_Resource_Db 的 init() 方法的开头添加var_dump($this->getDbAdapter()); exit; 以检查它
  • @littletipz 确保您已删除缓存(如果有),然后尝试。 Zend Framework 缓存它的类,application.ini 也可以被缓存。
【解决方案2】:

您应该将此功能添加到 Bootstrap 中

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

【讨论】:

    【解决方案3】:

    尝试添加这个

    SetEnv APPLICATION_ENV development
    

    在文件 PROJECTNAME\public\.htaccess 的末尾

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多