Soft Delete Implementation

I’ve implemented this soft delete behavior: https://github.com/amnah/yii2-classes, which works great.

However, it does nothing to prevent the user from loading soft deleted records when using the Active Record or Active Query.

Is a native soft delete behavior being considered / developed for Yii2?

Has there been any discussions for / against the implementation of such a thing?

(Manually adding ‘AND WHERE deleted_at = NULL’ on each and every query isn’t really a nice solution)

create some public function in your behavior like




public function showUnDeleted($query)

{

   // do some logic here

}


and call this method in the ActiveRecord where you attached your behavior to load un deleted records



I thought of that, but it will lead to lots of data fetched that will be discarded immediately.

My idea was to extend the Query model somehow, but I haven’t figured out if it’s possible to extend the Query without extending the Model, the ActiveRecord and the ActiveQuery aswell…

imaginary pseudocode:




public function onlyActive(){

    $this->andWhere('is_deleted', null);

    return $this;

}

Category::find()->where('something',$awesome)->onlyActive()->all();



Ah cool, it looks like they’ve actually added in some scope functionality recently. Have you looked at these potential solutions yet?

http://stuff.cebe.cc/yii2docs/guide-db-active-record.html#scopes

http://stuff.cebe.cc/yii2docs/guide-db-active-record.html#default-scope

This part is pretty important if you use the latter solution:

Note that all your queries should then not use where() but andWhere() and orWhere() to not override the default condition.