Strawberry
Strawberry

Reputation: 67868

How can I store more information in a session in Zend?

So I created a user system, and I want to grab the a few other fields from my database after authenticate()ing them. How can I do this?

Upvotes: 0

Views: 45

Answers (1)

fronzee
fronzee

Reputation: 1818

If you want to store extra information in the identity beyond the typical, you can do something like this upon successful authentication.

This is in my login controller, processAction() upon successful authentication:

$identity = new stdClass;
$identity->username = strtolower( $auth->getIdentity() );
$identity->miscParam1 = 'Miscellaneous parameter1';
$identity->miscParam2 = 'Miscellaneous parameter2';
# ... etc ...

$storage = $auth->getStorage();
$storage->write($identity);

It's important to note that if you use this approach, then every time you want to get the username or the other parameters, you will need to retrieve it like so:

$identity = Zend_Auth::getInstance()->getStorage()->read();
echo $identity->username; # echo is just for example
echo $identity->miscParam1;
echo $identity->miscParam2;

Note that you would use getStorage()->read() instead of getIdentity() (which might also work but I am not sure).

Now, the only thing left to do is populate $identity->miscParam1 (etc) with information from your database using Zend_Db_Select. (Looking back at your question... I hope you weren't asking how to query. I probably didn't even answer your question.)

Upvotes: 1

Related Questions