1

I ve been developing a facebook application on Yii and after the authentication i want to set the username and id according to the facebook username and id. As i am new to Yii I dont't have much idea about how to do it. Though when i tried to set the user id in my UserIdentity class, it gave an error saying that

Property "UserIdentity.id" is read only.

is there any other way to do it?

Rahul Thakur
  • 824
  • 1
  • 12
  • 27
  • maybe have a look at http://www.yiiframework.com/extension/facebook-connect/ – ldg Sep 20 '11 at 12:55
  • 1
    have a look at this http://stackoverflow.com/questions/7458940/yii-cuseridentity-vs-a-user-model/7459025#7459025 – bool.dev Sep 20 '11 at 16:10

2 Answers2

2

What are you using for authentication now? If you set up a webapp you should have a UserIdentity class in components that extends CUserIdentity. This is usually where you would use setState(), like $this->setState('first_name', $record->first_name); to set the user's first name from a db record after authentication, as an example, which you can retrieve via Yii::app()->user->first_name

However, I think what you may be looking for is Yii::app()->user->name = 'newname'; which changes the CWebUser property that CUserIdentity sets during authentication with the CUserIdentity::username value.

Either option is fine -- setting a custom Yii::app()->user->value or resetting the Yii::app()->user->name value, whichever works best for your app. (I wouldn't reset the user->id value tho.)

ldg
  • 9,112
  • 2
  • 29
  • 44
1

Well that post by DroidUser really helped to get a better understanding of CUserIdentity class. Though when I opened the CUserIdentity.php in Yiiroot/framework/ i found that only getter methods for reading id and username has been defined there but not to set them.

public function getId()
{
    return $this->username;
}
public function getName()
{
    return $this->username;
}

and that both of them return the username you logged in with, though i found a CWebUser class which is also extended by IUserIdentity. Here you can use

Yii::app()->user->setState('name',$value) 

to actually set a field and assign some value to it and a similar getState method to read it. I think this will solve my problem as all i want to do is to store some user identification values in a system-wide accessible variable. Any suggestions?

Rahul Thakur
  • 824
  • 1
  • 12
  • 27