Coder Social home page Coder Social logo

yii2-softdelete's Introduction

Yii2 Soft Delete

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

Soft delete behavior for Yii2.

Installation

The preferred way to install this extension is through composer.

Either run

$ composer require cornernote/yii2-softdelete "*"

or add

"cornernote/yii2-softdelete": "*"

to the require section of your composer.json file.

Usage

In your ActiveRecord class:

public function behaviors() {
    return [
        \cornernote\softdelete\SoftDeleteBehavior::className(),
        // or
        [
            'class' => \cornernote\softdelete\SoftDeleteBehavior::className(),
            'attribute' => 'deleted_time',
            'value' => new \yii\db\Expression('NOW()'), // for sqlite use - new \yii\db\Expression("date('now')")
        ],
    ];
}

then you can use explicitly $model->softDelete(), $model->hardDelete() and $model->unDelete() (for softly deleted models). Each of these methods return boolean result. Also $model->softDelete() calls indirectly from $model->delete(), which always returns false.

In your ActiveQuery class:

public function behaviors() {
    return [
        \cornernote\softdelete\SoftDeleteQueryBehavior::className(),
        // or
        [
            'class' => \cornernote\softdelete\SoftDeleteQueryBehavior::className(),
            'attribute' => 'deleted_time',
        ],
    ];
}

yii2-softdelete's People

Contributors

antontyutin avatar cornernote avatar davidjeddy avatar elvenpath avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

yii2-softdelete's Issues

Trying a query on joined tables using soft delete behavior throws sql error

Trying to join two or more tables that are using soft delete, same attribute name and deleted or notDeleted method fails with Column 'deleted_at' in where clause is ambiguous.

I just fixed it by prefixing the attribute name with the table name.
I'm going to do a pull request after posting this issue.

SoftDeleteQueryBehavior being ignored

Hey, Ive set softdelete behavior in an ActiveRecord model with the following config:

            TimestampBehavior::className(),
            'softDeleteBehavior' => [
                'class'                     => SoftDeleteBehavior::className(),
                'softDeleteAttributeValues' => [
                    'deleted_at' => new Expression('UNIX_TIMESTAMP()')
                ],
                'replaceRegularDelete'      => true
            ],

Its working: when I delete, "detelet_at" is set with the unix timestamp.

Now, to retrieve without the deleted ones, Ive set the query like this:

class UserQuery extends ActiveQuery
{

    public function behaviors()
    {
        return [
            'softDelete' => [
                'class' => SoftDeleteQueryBehavior::className(),
                'deletedCondition'    => [
                    'deleted_at' => new Expression('IS NOT NULL'),
                ],
                'notDeletedCondition' => [
                    'deleted_at' => new Expression('IS NULL'),
                ],
            ],
        ];
    }
}

Its being completely ignored, the deleted records are returned normally on any query

Im not sure its a bug, sorry if it inst. But everything seems right and I dont know what could be wrong.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.