Search code examples
yiicgridview

Set deleted records = 1 without actually deleting the records


How can i set the records i want to delete to 1 instead of deleting the record itself? I am using Yii framework right now and i am new to it. I have the CGridview with update, view and delete. On the delete i want the user to set the records which they want to delete to 1, instead of deleting the entire record. Thank you in advance for any help you may provide!


Solution

  • Just update your actionDelete in the controller:

    public function actionDelete($id)
        {
            $model = $this->loadModel($id);
            $model->is_deleted = "1"; //assuming is_deleted is the column name
            $model->update();
    
            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
    

    and your model search function to remove those items which have is_deleted = 1, you can:

    //.... rest of your code above
    $criteria->addCondition("is_deleted != 1");
    return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
            ));