如何将数据附加到Zend_Auth对象

时间:2013-04-19 07:14:41

标签: php zend-framework zend-auth

大家好日子,

我有以下代码将商店数据附加到Zend_Auth对象

    $auth        = Zend_Auth::getInstance();

    $dbAdapter   = Zend_Db_Table::getDefaultAdapter();

    $authAdapter = new Zend_Auth_Adapter_DbTable(
    $dbAdapter,
                'account', 
                'email', 
                'password',
                'delete_flag=0'
    );
    //MD5(?) AND  .. add this along with the prev where condn of delete flag...

    $authAdapter->setIdentity($loginDataArray['email'])
    ->setCredential($loginDataArray['password']);

    $result = $auth->authenticate($authAdapter); 
    var_dump($result);

    if ($result->isValid()) {

        $authStorage = $auth->getStorage();

        // the details you wan to store in the session
        $userDetails = array();         

        $userDetails['account_id']    = $account_id;
        $userDetails['email']      = $loginDataArray['email'];

        $authStorage->write($userDetails);
}

现在,我如何在会话的后期添加更多数据。我如何稍后编辑相同的Zend_Auth对象。

2 个答案:

答案 0 :(得分:1)

身份验证状态存储在已注册的Auth Storage中。默认情况下,这是Zend_Session。你可以通过这个

获得会话
$namespace = new Zend_Session_Namespace('Zend_Auth');
然后做这样的事情

$namespace->newname = "newvalue";

答案 1 :(得分:1)

好的,你没有'编辑'Zend_Auth身份。你有不同的身份。您可以通过Zend_Auth对象设置,读取,写入或清除存储。

然而,我们中的许多人将这些相同的数据用于各种显示目的,因此通常可以使用的解决方案是在存储标识时将数据设置为不同的会话命名空间或注册表项,或者只更新Zend_Auth创建的会话。 chandresh _ 建议。

if ($result->isValid()) {

        $authStorage = $auth->getStorage();

        // the details you wan to store in the session
        $userDetails = array();         

        $userDetails['account_id']    = $account_id;
        $userDetails['email']      = $loginDataArray['email'];
        //add user data to registry
        $user = Zend_Registry::set('user', $userDetails);

        $authStorage->write($userDetails);
}

如果你真的想做自己的事情,你可以通过实现Zend_Auth_Storage_Interface编写自己的存储适配器,或者你可以通过实现Zend_Auth_Adapter_Interface编写自己的Auth适配器并在适配器中包含存储组件。

很多选择,祝你好运。