model class not updating values in database

I am trying to update values in database using model class. My code goes below, could some one correct me where i am going wrong.




public function actionDelete($id)

{

     if(Yii::app()->request->isPostRequest){

        $model=UserProfile::model()->findByPk((int)$id);

        $model->status=1;

        $model->save();

     }

}



when i try to delete user from front end, in my database instead of deleted the record i am trying to update a column(status) value.

Is your PK of the model really an integer? Not a varchar?

yes my friend , you are updating value not delete … so the controller updates value :)

use below controller code instead of your current one ,


public function actionDelete($id)

	{

		if(Yii::app()->request->isPostRequest)

		{

			$this->loadModel($id)->delete();         //  Delete specific data related to id

                        ...

                        ...

                        ...

simple … … .

If you’ve defined a validation rule for status, perhaps it will fail?

Have a look at this post.

As an alternative, I think you should be able to use updateByPK (check the return value for exactly one row updated).

Edit: Of course you don’t have to use AR at all, DAO would be fine too.

/Tommy

if save() returns false, then vardump model->errors to see why

To expand a little on what el chief wrote;

It’s good practice to always throw an error with details if the save fails:




if(!$model->save())

    throw new Exception "Error saving UserProfile model : " . var_export($model->getErrors(), true);



I agree with Tri post.

Further reading http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules

Rule of thumbs, if you add new columns in your table after calling Gii, you need to add validation rules to make the saving works.

I have modified my rules to consider all the validations on create or update




public function rules()

{

     return array(

       array('email, password, confirm_password, realname', 'required','on'=>'create,update'),

       array('created_on, lastvisit, created_by, role_id, status', 'numerical', 'integerOnly'=>true),

       array('email, realname', 'length', 'max'=>70, 'min' => 7, 'message' => 'Incorrect Email (minimal length 7 symbols).','on'=>'create,update'),

       array('password', 'length', 'max'=>128,'min' => 7,'message' => 'Incorrect Password (minimal length 7 symbols).','on'=>'create,update'),

       array('confirm_password', 'compare', 'compareAttribute'=>'password', 'message' => 'Retype Password is incorrect.','on'=>'create,update'),

       array('email', 'unique', 'message' => 'This user\'s email address already exists.','on'=>'create,update'),

       array('realname', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => 'Incorrect symbols (A-z0-9).','on'=>'create,update'),

       array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>'create,update'),

       array('id, email, realname, created_on, lastvisit, created_by, role_id, status', 'safe', 'on'=>'search'),

     );

}



Even on delete method confirm_password,verifyCode fields are going for validations even though i have given ‘on’(create and update) parameter for all.