[Solved] UserController not saving data in salt field

I am learning yii framework by expanding the blog tutorial. To manage users, I created the UserController and User model. To generate encrypted password and salt, I modified the actionCreate() method of UserController as given here :




	public function actionCreate()

	{

		$model=new User;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

			// salt 

			srand(time()) ;

			$randval = rand(0,time());	// generate a random number generator 

			$mdsalt = md5($randval) ; 

			$_POST['User']['salt'] = $mdsalt ; 

			

			$newpassword = md5($mdsalt .$_POST['User']['password'] ) ;

			$_POST['User']['password'] =$newpassword; 

			

			//print_r($_POST['User']); 

			//return ; 

		

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

			if($model->save())

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

		}

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

			'model'=>$model,

		));

	}



Framework saves the new user record in tbl_user, however, the salt field in tbl_user is always NULL. If I print $_POST[‘User’], I do see data for salt, as shown here.


Array ( [username] => Testuser [password] => 9e0bc7638cd9329b009d877c88640b6a [email] => test@example.com [profile] => [firstname] => John [lastname] => Sample [title] => Sales Manager [userlevel] => 5 [salt] => bc0f672738c0ed66c0fbcac7ca4bfd61 )



Could someone please explain me why no data gets stored in the salt field?

I solved the problem by moving the password/salt creation code to beforeSave() function of User.php as shown here.


protected function beforeSave()

	{

		if(parent::beforeSave())

		{

			if($this->isNewRecord)	// Creating new record 

			{

				srand(time()) ;

				$randval = rand(0,time());	// generate a random number generator 

				$mdsalt = md5($randval) ; 

				$this->salt= $mdsalt;

				$this->password = md5($this->salt . $this->password ) ; 

				return true;

			}

			else

			// saving existing record 

			// $this->update_time=time();

			return true;

		}

		else

			return false;

	}	   




hi,

i followed your above it works wonderfully alongwith the yii blog user authentication method. there is one glich, however. there is no id created for user table.

tbl_user.id has AUTO_INCREMENT for the default value. Therefore, the ID should be created when a new record is inserted in tbl_user. Please check the default value for the id field.