Question

I have a yii2 project, i've used gii to generate crud operations for a model named account.

now i want to change(or add a new one) the delete function so that it doesn't delete the record from the database but instead flag it as deleted, and i'm not sure where to make my changes.

Was it helpful?

Solution 2

Add this in actionDelete of your controller.

$model->deleted = 1;
$model->save();

OTHER TIPS

You should do this in model's code itself, by overriding beforeDelete() event. Something like this:

public function beforeDelete()
{
    if (parent::beforeDelete()) {
        $this->deleted = true;
        $this->save();

        return false;
    } else {
        return false;
    }
}

(written from my memory, not tested, but should work)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top