Zend_Auth:不工作,getDbSelect()为空

时间:2009-09-23 06:41:00

标签: zend-framework zend-auth

我的身份验证失败,当我尝试getDbSelect()时,我什么都没得到......我做错了什么?

class AuthController extends Zend_Controller_Action
{
    protected $auth;

    public function init()
    {
        $db = $this->getInvokeArg("bootstrap")->getResource("db");
        $this->auth = new Zend_Auth_Adapter_DbTable($db, 'users', 'username', 'password', 'md5(?)');
    }
    public function loginAction()
    {
        if (isset($_POST["username"])) {
            $result = $this->auth
                ->setIdentity($this->getRequest()->getParam('username'))
                ->setCredential($this->getRequest()->getParam('password'));
            echo " > " . $this->auth->getDbSelect();die();
            $result = $this->auth
                ->setIdentity($this->getRequest()->getParam('username'))
                ->setCredential($this->getRequest()->getParam('password'))
                ->authenticate();
            if ($result->isValid()) {
                $this->_redirect("postLogin");
            }
        }
    }

更新1:

我也试过没有MVC和我更熟悉的MySQL:

// setup auto loading
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

// setup db connection
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'     => '127.0.0.1',
    'username' => 'root',
    'password' => '',
    'dbname'   => 'learnZendAuth'
));

// create authentication adapter
$auth = new Zend_Auth_Adapter_DbTable($db, 'users', 'username', 'password', 'md5(?)');

// do login if request is posted
if (isset($_POST) && !empty($_POST)) {
    // do authencation
    $result = $auth->setIdentity($_POST['username'])
         ->setCredential($_POST['password'])
         ->authenticate();

    // check authentication valid
    if ($result->isValid()) {
        // login success
        echo "successfully logged in as: " . $result->getIdentity();
    } else {
        // login fail
        echo "
";
        print_r($result->getMessages());
        echo "
"; } } else { // request not posted // see if already logged in if (Zend_Auth::getInstance()->hasIdentity()) { echo "already logged in as: " . Zend_Auth::getInstance()->getIdentity(); } }

我得到了

  

致命错误:未捕获的异常   'Zend_Auth_Adapter_Exception'用   message'提供的参数   Zend_Auth_Adapter_DbTable失败了   请生成一个有效的sql语句   检查表和列名称   有效性“。在C:\ Program Files中   (86)\ Zend的\ ZendServer对其进行访问\共享\ ZendFramework \库\ Zend的\验证\适配器\ DbTable.php:414   堆栈跟踪:#0 C:\ Program Files   (86)\ Zend的\ ZendServer对其进行访问\共享\ ZendFramework \库\ Zend的\验证\适配器\ DbTable.php(306):   Zend_Auth_Adapter_DbTable-> _authenticateQuerySelect(对象(Zend_Db_Select对象))

     

1 C:\ Program Files(x86)\ Zend \ Apache2 \ htdocs \ learnZf \ index.php(23):

     

Zend_Auth_Adapter_DbTable->认证()

     

2 {main}抛出C:\ Program Files(x86)\ Zend \ ZendServer \ share \ ZendFramework \ library \ Zend \ Auth \ Adapter \ DbTable.php

     第414行

2 个答案:

答案 0 :(得分:0)

尝试在有效语句中移动getDbSelect, 我通常也会使用

$userInfo = $this->auth->getResultRowObject();

获取用户信息,从未见过这个getDbSelect方法

答案 1 :(得分:0)

哦,我发现了我做错了什么......

第一次尝试,我使用了Sqlite,它不支持md5(?)处理

第二次尝试,我指定了错误的列名... name vs username ...