How Change Username By Yii::app()->User->Setname($This->Username)

Hi

i wanna let the users to change their ‘username’ any time. (like Twitter website)

i use ‘CDbHttpSession’ to save session in DB.

After update the username in DB, i should update the username in session using the code bellow.




class User extends CActiveRecord

{

...

	/**

	 * This is invoked after the record is saved.

	 */

	protected function afterSave()

	{

		parent::afterSave();

		if (strcmp(Yii::app()->user->name,$this->username) <> 0) {

			// Update session since username has changed

			Yii::app()->user->setName($this->username);

		}


	}

}



But above code is note reliable to update Yii::app()->user->name !

since, some times it doesn’t change Yii::app()->user->name without any error! and i see this situation:

[color="#2E8B57"]in DB: username= new-username[/color]

[color="#FF0000"]in session: name= old-username (Not Updated!)[/color]

could you please help me to find the solution?

[color="#483D8B"]

[b] Note: This problem occurs when i perform AjaxValidation.

and when i turn off the Ajax Validation, i have no problem and every thing works well.

my form is default Gii generated form and also controller is shown in my post below.

[/b][/color]

I do exactly the same - but with CHttpSession - and it works without any issues.

Do


var_dump(Yii::app()->user->name)

before/after assigning the new name. Or better: debug.

See my comment in the guidelines.

Thanks for your care.

i did a trace.

[color="#483D8B"]

[b] Note: This problem occurs when i perform AjaxValidation.

and when i turn off the Ajax Validation, i have no problem and every thing works well.

my form is default Gii generated form and also controller is shown in my post below.

[/b][/color]

for better understanding of trace process, i put the following code.

(the results are the same, when i apply new ‘name’ both in model(above code) or userController(following code))




class UserController extends Controller

{

...

	public function actionUpdate()

	{

		

		$model=$this->loadModel(Yii::app()->user->id);

		

		// Uncomment the following line if AJAX validation is needed

		$this->performAjaxValidation($model);


		if(isset($_POST['User']))

		{

			$model->attributes=$_POST['User'];

			if($model->save()) {

				// Before

				echo Yii::app()->user->name;

				echo $model->username;

				//

				if (strcmp(Yii::app()->user->name,$model->username) != 0) {

					// Update session since username has changed

					Yii::app()->user->setName($model->username);

				}

				// After

				echo Yii::app()->user->name;

				echo $model->username;

				//

			//	$this->redirect(array('view','id'=>$model->id));

				Yii::app()->end();

			}

		}


		$this->render('update',array(

			'model'=>$model,

		));

...

}



Before assigning the new name

Yii::app()->user->name= old_username

$model->username= new_username


After assigning the new name (Execute above code)


 After:  Yii::app()->user->setName($model->username);

[color="#FF0000"]Yii::app()->user->name= new_username[/color]

$model->username= new_username

But when i check the DB (user & session tables), i see:

DB.session-table: [color="#FF0000"]__name= old_username[/color]

DB.user-table.username= new_username


and when i trace above variables, after loading another page. the values showed below, confirms that the new ‘name’ is not applied in the session table!

[color="#FF0000"]

Yii::app()->user->name= old_username[/color]

$model->username= new_username

Note:

Username contains only "a-Z", "0-9" and "."

Example: J.Smith.7

And you’re shure, the line


Yii::app()->user->setName($model->username);

is always executed?




 if (strcmp(Yii::app()->user->name,$model->username) != 0) {

      echo 'Updating user sessionname'; //<----------------------------

      Yii::app()->user->setName($model->username);

  }




Try with !==


if (strcmp(Yii::app()->user->name,$model->username) !== 0)

Try with != instead of strcmp (I use this to compare):


if (Yii::app()->user->name != $model->username)

Otherwise it must be a general problem with the session, because

Yii::app()->user->setName($model->username) calls

user->setName()

calls

user->setState()

I would debug through the frameworks code by setting a breakpoint at user->setName() or var_dump there (undo the var_dump in the core after problem solved).

And: Why don’t you use a debugger? Debugging did help me a lot with such strange issues.

Dear Friend

Thanks again for your care and great advices too.

Ok.

about questions, Yes! the line


Yii::app()->user->setName($model->username);

is always executed.

this problem occurs only when i turn on "ajax validation". i try to do more traces and debug.

thanks agan and again.

[color="#2E8B57"]Thanks God. Problem is solved.[/color]

i put the solution here, to help other users may experiment such failure later.

i was using the default Gii generated form with the following header:




<?php

/* @var $this UserController */

/* @var $model User */

/* @var $form CActiveForm */

?>


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'user-form',

	// Please note: When you enable ajax validation, make sure the corresponding

	// controller action is handling ajax validation correctly.

	// There is a call to performAjaxValidation() commented in generated controller code.

	// See class documentation of CActiveForm for details on this.

	'enableAjaxValidation'=>true,

)); ?>


...



i chenged the code as below and then every thing goes well.




<?php

/* @var $this UserController */

/* @var $model User */

/* @var $form CActiveForm */

?>


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'user-form',

	// Please note: When you enable ajax validation, make sure the corresponding

	// controller action is handling ajax validation correctly.

	// There is a call to performAjaxValidation() commented in generated controller code.

	// See class documentation of CActiveForm for details on this.

	'enableAjaxValidation'=>true,

	'enableClientValidation'=>true,

	'clientOptions'=>array(

			'validateOnSubmit'=>true,

	),

)); ?>


...



Great thanks to "Joblo" for his kind help and companion in this case.