sbilyalov/yii2-softdelete

There is no license information available for the latest version (1.0.0) of this package.

Soft delete behavior for Yii2 framework

Installs: 275

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 0

Forks: 1

Open Issues: 0

Type:yii2-behavior

1.0.0 2016-03-23 12:16 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:36:58 UTC


README

This behaviour added soft-delete functionality to your active record

##Installation

The preferred way to install this extension is through composer.

Run:

php composer.phar require  --prefer-dist sbilyalov/yii2-softdelete "*"

or add

"sbilyalov/yii2-softdelete": "*"

to your composer.json file.

Usage

use sbilyalov\yii2\behaviors\SoftDelete;

public function behaviors ()
{
    return [
        SoftDeleteBehavior::className()
    ];
}

By default the SoftDelete behavior fills the is_deleted attribute with the number - 1

If your attribute names are different or you want to use a different way of mark deleted record you may configure the [[attribute]] and [[value]] properties like the following:

use sbilyalov\yii2\behaviors\SoftDelete;
use yii\db\Expression;

public function behaviors ()
{
    return [
        [
            'class' => SoftDeleteBehavior::className(),
            'attribute' => 'deleted_time',
            'value' => new Expression('NOW()'),
            'restoreValue' => null
        ]
    ];
}

Additional functions for active record model

// soft delete model
$model->remove();

// delete soft-deleted model from database
$model->forceDelete();

// restore soft-deleted model
$model->restore();

// call SoftDelete::remove()
$model->delete();