CWebUser property not defined

So frustrated.

Finally got my login system working and I am using a clientID foreign key in a default scope to limit a user to their own client’s data…

However, just now today, I am getting an Exception when I login:

"Property CWebUser.clientId is not defined"

This was working perfectly an hour ago and I did not touch any login related code.

I am using this property in a beforeSave() and scopes function. If I comment out those functions, I can login.

If I login and then echo out the value of:


I see the expected value. It is obviously being set after login. Why is my code suddenly barfing on the clientID not being set? Does this have something to do with the order in which Yii loads stuff??


Did you for instance declare a db AR relationship involving that property in a condition?


Sloppy reading by me. You mention that the problem goes away if you comment out beforeSave and scope. The scope seems more probable then, or do you try to save data before logging in.


Yii::App()->user->clientId is stored in the session, isn’t it?

If it times out, it isn’t set any more.

Could it be that?

That has to be the problem.

What I don’t get is why Yii loads the user model (which contains those methods) before the login process is complete.

Maybe I should split the user model from the login process entirely. Its kind of a chicken and the egg type problem.


I had to put a condition in my defaultscope function to check if the user was a guest. If not a guest, then condition is defined. If a guest (IE, not logged in), you need to pass an empty array to defaultscope.

	public function defaultScope()

	/* defaultScope is used to enforce clientID (site) context. Will only retrieve data with that user's site ID.

	*  User clientID (site) is set during login process in /components/UserIdentity class



		if(!Yii::App()->user->isGuest) {

		return array('condition'=>"clientId='".Yii::App()->User->clientId."'");

		} else { 

			//return array('condition'=>''); 

			return array();





[b]waitforit thank you so much :) to share your code, You fix my problem too ,


If the code not working, try to use isset()

if(!isset(Yii::App()->user->isGuest)) {