change the username of a user in yii? possible with default auth system?

ok, i imagine this might be a very newbish question.

I want to know if its possible to add a function to change the username in the build in yii user auth system?

i mean i don’t want to change the username in the demo, i want the user to be able to change his username after he has registered, but im worried that yii references the username as the userid, and thus will not allow to change it later on?

is this so? or am i worrying without good reason?

Why not? Yii::app()->user->setState(’__name’, $newName);

haha, thats one way.

i mean change it in the users database, not the session database.

the question im asking is

does yii rely on the username, or the user id to reference the user?

for example a user register’s, posts a comment, and then changes his name.


Yii::app()->user->id gives me the username of the user, what to do so that it gives me the id of the user? (and how to use id as the id instead of the username?)

extend CUserIdentity::getId()

im sorry but im completely new to frameworks, could you clarify what you mean by that?


i looked into your blog, and i see what you mean by that, but how does it help me get the user id?

could you post a more complete example?

If you used yiic to generate your code you should have a class called UserIdentity somewhere… Within that class, you need something like this:

        public function getId()


            //return $this->userId


You may use this as reference:

thank you!!

another little question if you don’t mind (i don’t want to make another thread, im making too many already), i notice that you made this skeleton app (nice job), but how do i make the app use the “users” database table instead of the “User” database table?


ok i made it work, by changing the name of models/User.php to models/Users.php and its name to (in the User.php file)

Users extends CActiveRecord {

and this bit

$record = User::model()->find($criteria); 


$record = Users::model()->find($criteria);

in the components/UserIdentity.php

you didn’t have to do that much. you just needed to change the table name in the model file

you mean just

Users extends CActiveRecord {

in models/user.php?

no, there is a method you can overwrite in the model called tableName()

Make it return the desired table name

thats all well and good, but i have been trying to add it for the last couple of days, and i can’t figure out where i need to add


i dont know what piece of code i need to add in god knows what file…

everywhere it says

or something similar, no where does it say what the override the tableName() method is or how to override it…


finnaly!! finnaly do i find one useful reference to tableName()

as always, no one told or explained that to have a differently named table you need to add this in the models/yourmodel.php file

class yourmodel extends CActiveRecord


        public static function model($className=__CLASS__)


                return parent::model($className);


        public function tableName()


                return 'YOURDATABASETABLENAME';


is it that hard to simply give that small piece of text below the description of the tableName() method??

What can’t you understand from the line ‘{return} string: the tablename’? If the usage was presented for all methods, API would be a bloated, unusable mess of snippets.

There are better ways to improve the documentation I suppose.

to be honest i don’t understand anything from the line ‘{return} string: the tablename’

i don’t even know what you mean by that :S

usage should be presented for each and every snippet, like the php documentation.