5

How do I delete all session variables at once if they are not in Array?

PS I set them this way:

$this->getUser()->setAttribute('PayPalTransaction.hash', $request->getParameter('hash'));

Regards, Roman

Roman Newaza
  • 11,405
  • 11
  • 58
  • 89

3 Answers3

10

The sfUser class (which you get with $this->getUser()), keeps all it's attributes in a sfNamespacedParameterHolder. So the setAttribute() function on sfUser if merely a proxy to the sfNamespacedParameterHolder::setAttribute(). You can get the reference to this holder with sfUser::getAttributeHolder().

The sfNamespacedParameterHolder also has a function clear(), which clears all attributes.

So to clear all attributes, use: $this->getUser()->getAttributeHolder()->clear().

(Please note that you will still be authenticated (e.g. logged in) when you clear the attribute holder).

Grad van Horck
  • 4,488
  • 1
  • 21
  • 22
  • Grad, if I store session in Array, I lose the ability of direct access, isn't it? E.g. I cannot easily change value or just use getAttribute() to refer to certain element... – Roman Newaza Sep 20 '11 at 08:40
  • Ehm? Could you clarify what you're trying to do? (Or open a new question, so everyone can answer) – Grad van Horck Sep 20 '11 at 12:56
8

Another way if you want to remove just one session variable not all of them is to use the following code

$this->getUser()->getAttributeHolder()->remove('att_name');

Again this will only remove one not all ... to clear all use the previous code by Grad

IslamAlaa
  • 91
  • 2
7

To remove all attributes of a namespace :

$this->getUser()->getAttributeHolder()->removeNamespace('yournamespace');
arsenik
  • 987
  • 2
  • 8
  • 22