Need help when update or create action

Dear All,

I have a table that have these columns :

  • id

  • username

  • password

so I create a model with extending to CActiveRecord,

when create or update action, I want to adding password2 to confirm the password which password2 is not of part of the table.

how to do it?

thank you for your attention

You can add it to the model:




class Model extends CActiveRecord

{


   public $password2;


}



Great sir… thank you. I wanna to ask again sir.

the password is saved as hash.

when I update. the password field is filled with hash automatically. but it should be keep empty.

how to do this sir?

sorry for my lack of knowledge sir…

but thank you very much…

hi sir, to handle this problem in hash password. I have try this way:

I make one public property in Model


Users extends CActiveRecord {

   public $password1;

   public $password2

}

in _form.php

I change


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'password'); ?>

		<?php echo CHtml::activePasswordField($model,'password'); ?>

	</div>

to


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'password1'); ?>

		<?php echo CHtml::activePasswordField($model,'password1'); ?>

	</div>

in controller I change too


        public function actionUpdate_Process(){

            if(isset($_POST['Users'])){

                $model = $this->loadModel($_POST['Users']['user_id']);

                $model->setAttributes($_POST['Users']);

                $model->edited_time = time();

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

                $model->password = UserIdentity::hash($_POST['Users']['password']); // I change this

                if($model->save()){

                    Yii::app()->user->setFlash('error', 'Saved');

                    $this->redirect(array('users/admin'));

                }else{

                    Yii::app()->user->setFlash('error', CHtml::errorSummary($model));

                    $this->redirect(array('users/update', 'id' => $model->user_id));

                }

            }

        }

to


        public function actionUpdate_Process(){

            if(isset($_POST['Users'])){

                $model = $this->loadModel($_POST['Users']['user_id']);

                $model->setAttributes($_POST['Users']);

                $model->edited_time = time();

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

                $model->password = UserIdentity::hash($_POST['Users']['password1']); // I change this

                if($model->save()){

                    Yii::app()->user->setFlash('error', 'Saved');

                    $this->redirect(array('users/admin'));

                }else{

                    Yii::app()->user->setFlash('error', CHtml::errorSummary($model));

                    $this->redirect(array('users/update', 'id' => $model->user_id));

                }

            }

        }

are this way is correct sir? or there are more better way?

thanks

Looks good. Note that you can also do the password-hashing and so on in the beforeSave() method of CActiveRecord. You could do something like this:




public function beforeSave()

{


   if (parent::beforeSave())

   {


      if ($this->isNewRecord)

      {

         $this->password = sha1($this->password);

         ...

      }

      else

      {

         $this->edited_time = time();

         ...

      }


      return true;


   }


}



thanks sir.

I want to ask again sir.

the problem still same, but little different.

say I have 2 colums : username & password.

to update I using this code

update


public function actionUpdate(){

  $model = Users::model()->loadModel($_POST['Users']['id']);

  $model->setAttributes($_POST['Users']);

  $model->save();

}

this code will saving username & password column together.

that I want is, I just saving username colum and ignore password column.

how to do this sir?

I have spend about more than 3 hours, to resolving this with my self. but was failed :frowning:

please help…

thank you very much…

To only save the username you can do:


$model->update(array('username'));

But note that no validation is performed. If you need validation of the username you can do:


if ($model->validate(array('username')))

{

   $model->update(array('username'));

}

thank you very much sir…:slight_smile:

I want asking again sir,

when I insert new record that existing in database. is always outputting error CException


CDbException

Description


CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'didin' for key 'username'INSERT INTO `tbx_site_users` (`username`, `email`, `user_level_id`, `status`, `first_name`, `last_name`, `display_name`, `password`, `created_time`, `created_by`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7, :yp8, :yp9)

Source File


/mnt/win_d/www/website/latihan/yii/framework/db/CDbCommand.php(227)

is it possible to mute this error sir?

thanks

Error says that you have already inserted "didin" username. In model`s rules method define unique for username field:


public function rules()

{

    return array(

        array('username', 'unique', 'on'=>'register'),

        ...

    );

 }

Look in: validators

thanks sir, that is work.

I get error again when I using beforeSave() method and beforeValidate() method.


public function beforeSave(){

   if(parent::beforeSave(){

      if($this->isNewRecord){

         $this->created_time = time();

      }else{

         $this->edited_time = time();

      }

   }

}

but when I var_dump($this->getAttributes()), created_time or edited_time still empty. I have tried to implements the code above in beforeValidate(), but always same result.

but, when I put at process method, say the process method is actionUpdate_Process(), it works fine…

please help me…

thanks.