Losing behaviors

I have an controller action that attaches a behavior to the CWebUser object of Yii::app (Yii::app()->user). I attached it using Yii::app()->user->attachBehavior. Example:

Yii::app()->user->attachBehavior(‘newbehavior’, new NewBehavior);

Later, in another controller action, the behavior is missing from the CWebUser object (the CComponent::_m array is null). I verified after attaching the behavior that it is attached properly and was able to call its methods after attachment. I don’t understand how this is happening. Any ideas?


Do you mean, you attach the behavior in one request and want to use it in another request? That will not work. Behaviors are not persistent. If you always need the behavior attached, you can configure in your main.php through the behaviors property of your user component.

Thanks, I changed the code according to your explanation and everything is working.