Download the PHP package yii1tech/ar-softdelete without Composer

On this page you can find all versions of the php package yii1tech/ar-softdelete. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package ar-softdelete

Application Runtime Configuration Extension for Yii 1


This extension provides support for Yii1 ActiveRecord soft delete.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

or add

to the "require" section of your composer.json.

Usage

This extension provides support for so called "soft" deletion of the ActiveRecord, which means record is not deleted from database, but marked with some flag or status, which indicates it is no longer active, instead.

This extension provides \yii1tech\ar\softdelete\SoftDeleteBehavior ActiveRecord behavior for such solution support in Yii1. You may attach it to your model class in the following way:

There are 2 ways of "soft" delete applying:

Usage of softDelete() is recommended, since it allows marking the record as "deleted", while leaving regular delete() method intact, which allows you to perform "hard" delete if necessary. For example:

However, you may want to mutate regular ActiveRecord delete() method in the way it performs "soft" deleting instead of actual removing of the record. It is a common solution in such cases as applying "soft" delete functionality for existing code. For such functionality you should enable \yii1tech\ar\softdelete\SoftDeleteBehavior::$replaceRegularDelete option in behavior configuration:

Now invocation of the delete() method will mark record as "deleted" instead of removing it:

Querying "soft" deleted records

Obviously, in order to find only "deleted" or only "active" records you should add corresponding condition to your search query:

However, you can use \yii1tech\ar\softdelete\SoftDeleteQueryBehavior to facilitate composition of such queries. Once being attached ite provides methods similar to scopes for the records filtering using "soft" deleted criteria. For example:

You may easily create listing filter for "deleted" records using filterDeleted() method:

This method applies notDeleted() scope on empty filter value, deleted() - on positive filter value, and no scope (e.g. show both "deleted" and "active" records) on negative (zero) value.

Note: `yii1tech\ar\softdelete\SoftDeleteQueryBehavior1 has been designed to properly handle joins and avoid ambiguous column errors, however, there still can be cases, which it will be unable to handle properly. Be prepared to specify "soft deleted" conditions manually in case you are writing complex query, involving several tables with "soft delete" feature.

By default yii1tech\ar\softdelete\SoftDeleteQueryBehavior composes filter criteria for its scopes using the information from yii1tech\ar\softdelete\SoftDeleteBehavior::$softDeleteAttributeValues. Thus, you may need to manually configure filter conditions in case you are using sophisticated logic for "soft" deleted records marking. For example:

Tip: you may apply a condition, which filters "not deleted" records, to the search query as default, enabling yii1tech\ar\softdelete\SoftDeleteBehavior::$autoApplyNotDeletedCondition.

Smart deletion

Usually "soft" deleting feature is used to prevent the database history loss, ensuring data, which been in use and perhaps have a references or dependencies, is kept in the system. However, sometimes actual deleting is allowed for such data as well. For example: usually user account records should not be deleted but only marked as "inactive", however if you browse through users list and found accounts, which has been registered long ago, but don't have at least single log-in in the system, these records have no value for the history and can be removed from database to save disk space.

You can make "soft" deletion to be "smart" and detect, if the record can be removed from the database or only marked as "deleted". This can be done via \yii1tech\ar\softdelete\SoftDeleteBehavior::$allowDeleteCallback. For example:

\yii1tech\ar\softdelete\SoftDeleteBehavior::$allowDeleteCallback logic is applied in case \yii1tech\ar\softdelete\SoftDeleteBehavior::$replaceRegularDelete is enabled as well.

Handling foreign key constraints

In case of usage of the relational database, which supports foreign keys, like MySQL, PostgreSQL etc., "soft" deletion is widely used for keeping foreign keys consistence. For example: if user performs a purchase at the online shop, information about this purchase should remain in the system for the future bookkeeping. The DDL for such data structure may look like following one:

Thus, while set up a foreign key from 'purchase' to 'user', 'ON DELETE RESTRICT' mode is used. So on attempt to delete a user record, which have at least one purchase, a database error will occur. However, if user record have no external reference, it can be deleted.

Usage of \yii1tech\ar\softdelete\SoftDeleteBehavior::$allowDeleteCallback for such use case is not very practical. It will require performing extra queries to determine, if external references exist or not, eliminating the benefits of the foreign keys database feature.

Method \yii1tech\ar\softdelete\SoftDeleteBehavior::safeDelete() attempts to invoke regular CBaseActiveRecord::delete() method, and, if it fails with exception, falls back to yii1tech\ar\softdelete\SoftDeleteBehavior::softDelete().

By default safeDelete() method catches \CDbException exception, which means soft deleting will be performed on foreign constraint violation DB exception. You may specify another exception class here to customize fallback error level. For example: usage of \Throwable will cause soft-delete fallback on any error during regular deleting.

Record restoration

At some point you may want to "restore" records, which have been marked as "deleted" in the past. You may use restore() method for this:

By default, attribute values, which should be applied for record restoration are automatically detected from \yii1tech\ar\softdelete\SoftDeleteBehavior::$softDeleteAttributeValues, however it is better you specify them explicitly via \yii1tech\ar\softdelete\SoftDeleteBehavior::$restoreAttributeValues.

Tip: if you enable \yii1tech\ar\softdelete\SoftDeleteBehavior::$useRestoreAttributeValuesAsDefaults, attribute values, which marks restored record, will be automatically applied at new record insertion.

Events

By default \yii1tech\ar\softdelete\SoftDeleteBehavior::softDelete() triggers \CActiveRecord::onBeforeDelete and \CActiveRecord::onAfterDelete events in the same way they are triggered at regular delete().

Also \yii1tech\ar\softdelete\SoftDeleteBehavior allows you to hook on soft-delete process defining specific methods at the owner ActiveRecord class:

For example:


All versions of ar-softdelete with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1
yiisoft/yii Version ~1.1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package yii1tech/ar-softdelete contains the following files

Loading the files please wait ....