0

I've got some data with one of the fields called activity. Activity can be either 0 (inactive) or 1 (active) in the database.

I have the delete button next to each row of data in my index page and would like that, when it is clicked, if activity is set to 1, i would like it to be set to 0.

So basically the delete does not delete anything, it just switches the activity value from 1 to 0. I have some code also which checks if activity is already set to 0 and prevents from further deletion.

How can I do this pls?

This is the code in my controller;

    public function actionDelete1($id)
{
    $this->findModel($id)->delete();
   

    return $this->redirect(['index']);

}



public function actionRetire($id)
{
    $model = $this->findModel($id);
    $model->activity = 0;
    $model->save();
    
    return $this->redirect(['index']);
}

This is the code in my model:

 public function canRetire($id)
{
   if(!$this->checkIfRetired($id)
    return true;
   else return false;
}

public function checkIfRetired($id)
{
    if(JobPlanning::find()->where([ 'and', 'activity=:activity AND 
    id=:id'],array(':activity'=>1,':id'=>$id))->count() >0) 
        return false;
    else
        return true;
}

And the delete button in my index page:

'delete' => function ($url, $model, $key) {
                        return $model->canRetire($model['id'])?Html::a(Html::tag('span', '', ['class' => "glyphicon glyphicon-trash"]), $url, ['title' => Yii::t('yii', 'Delete'), 'data' => ['method' => 'post', 'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), 'params' => ['id' => $model['id'], '_csrf' => Yii::$app->request->csrfToken]]]):'';

if ($action === 'delete') {
                         $url = 'index.php?r=admin/jobplanning/retire&id=' . $model['id'];
                        return $url;
                    }
                    
1
  • Wouldn't update your-table set activity=0 where id=:id do it for you? I'm not sure if there's much point checking whether it's already marked inactive - why would it matter if a user was to somehow mark an already-inactive user as inactive again? Unless it's doing other things as part of the process, of course.
    – droopsnoot
    Sep 10, 2020 at 8:21

2 Answers 2

3

This is just a soft delete then: no actual row is deleted but "marked". Two parts for this code:

Controller


public function behaviors()
{
    return [
        // ....
        'verbs' => [
            'class' => \yii\filters\VerbFilter::className(),
            'actions' => [
                'delete' => ['POST'],
            ],
        ],
        // ....
    ];
}


public function actionDelete($id){
    $model = $this->findModel($id);
    $model->activity = 0;
    if(!$model->save()){
        //throw exception
    }
    return $this->redirect(['view', 'id' => $id]);
}

the button


'delete' => function($url, $model, $key){
    return $model->activity == 1
        ? Html::a('DELETE ICON', $url, ['data-method' => 'post', 'data-confirm' => 'are you sure...'])
        : "";
}

Note I have defined the action as just accesible when called with POST method because this is going to change the state of data. You can force a POST link with the data-method => 'post' passed to the options parameter of the Html::a() helper function.

To totally prevent deletion via ActiveRecord operations override beforeDelete on the model class:

public function beforeDetete(){
     return false;
}

I think you don't need extra methods.

0

Why dont you try the following code? And then edit your code in the view part as required to call the api.

public function actionRetire($id, $status)
{
    $model = $this->findModel($id);
    //$model->activity = !$model->activity;
    $model->activity = !$status;
    if($model->save())
    \Yii::$app->getSession()->setFlash('success', 'Succesfully updated);


    
    return $this->redirect(['index']);
}

PS: Since you dont want to disable the toggle, its better to pass the status from the view through the request, there by making it stateless. I have updated the code to do that.

3
  • This is for if you want to simply toggle the data. You can sanitize the $id or check for exception handling as per your use case.
    – K3V
    Sep 10, 2020 at 13:29
  • what happens if this action is called again? it will toggle the activity attribute. Perhaps the name of the action in the example could be improved. Sep 25, 2020 at 15:50
  • 1
    the code in my answer is updated. this is just a soft delete. Instead of delete, set a field marking this instance is not usable anymore: in this case activity. There is no way to restore it. Perhaps the instances with activity == 0 should not be listed anymore. Anyway, via controller is not possible to delete this model. Other check could be done in beforeDelete() of the class: return false. This makes impossible to delete it via ActiveRecord operations. Sep 28, 2020 at 0:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.