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;
}
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.