PHP Class yii2tech\ar\softdelete\SoftDeleteBehavior

php class Item extends ActiveRecord { public function behaviors() { return [ 'softDeleteBehavior' => [ 'class' => SoftDeleteBehavior::className(), 'softDeleteAttributeValues' => [ 'isDeleted' => true ], ], ]; } }
Since: 1.0
Author: Paul Klimov ([email protected])
Inheritance: extends yii\base\Behavior
Show file Open project: yii2tech/ar-softdelete Class Usage Examples

Public Properties

Property Type Description
$allowDeleteCallback callback, which execution determines if record should be "hard" deleted instead of being marked as deleted. Callback should match following signature: boolean function(BaseActiveRecord $model) For example: php function ($user) { return $user->lastLoginDate === null; }
$deleteFallbackException class name of the exception, which should trigger a fallback to SoftDeleteBehavior::softDelete method from SoftDeleteBehavior::safeDelete. By default [[\yii\db\IntegrityException]] is used, 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 [[\Exception]] will cause soft-delete fallback on any error during regular deleting.
$invokeDeleteEvents whether to invoke owner [[BaseActiveRecord::beforeDelete()]] and [[BaseActiveRecord::afterDelete()]] while performing soft delete. This option affects only SoftDeleteBehavior::softDelete method.
$restoreAttributeValues values of the owner attributes, which should be applied on restoration from "deleted" state, in format: [attributeName => attributeValue]. If not set value will be automatically detected from [[softDeleteAttributeValues]].
$softDeleteAttributeValues values of the owner attributes, which should be applied on soft delete, in format: [attributeName => attributeValue]. Those may raise a flag: php ['isDeleted' => true] or switch status: php ['statusId' => Item::STATUS_DELETED] Attribute value can be a callable: php ['isDeleted' => function ($model) {return time()}]

Public Methods

Method Description
afterRestore ( ) This method is invoked after record is restored from "deleted" state.
afterSoftDelete ( ) This method is invoked after soft deleting a record.
beforeDelete ( ModelEvent $event ) Handles owner 'beforeDelete' owner event, applying soft delete and preventing actual deleting.
beforeRestore ( ) : boolean This method is invoked before record is restored from "deleted" state.
beforeSoftDelete ( ) : boolean This method is invoked before soft deleting a record.
events ( )
getReplaceRegularDelete ( ) : boolean
restore ( ) : integer | false Restores record from "deleted" state, after it has been "soft" deleted.
safeDelete ( ) : false | integer Attempts to perform regular [[BaseActiveRecord::delete()]], if it fails with exception, falls back to SoftDeleteBehavior::softDelete.
setReplaceRegularDelete ( boolean $replaceRegularDelete )
softDelete ( ) : integer | false Marks the owner as deleted.

Protected Methods

Method Description
isDeleteAllowed ( ) : boolean
restoreInternal ( ) : integer
softDeleteInternal ( ) : integer | false Marks the owner as deleted.

Private Methods

Method Description
beginTransaction ( ) : Transaction | null Begins new database transaction if owner allows it.

Method Details

afterRestore() public method

The default implementation raises the [[EVENT_AFTER_RESTORE]] event. You may override this method to do postprocessing after the record is restored. Make sure you call the parent implementation so that the event is raised properly.
public afterRestore ( )

afterSoftDelete() public method

The default implementation raises the [[EVENT_AFTER_SOFT_DELETE]] event. You may override this method to do postprocessing after the record is deleted. Make sure you call the parent implementation so that the event is raised properly.
public afterSoftDelete ( )

beforeDelete() public method

Handles owner 'beforeDelete' owner event, applying soft delete and preventing actual deleting.
public beforeDelete ( ModelEvent $event )
$event yii\base\ModelEvent event instance.

beforeRestore() public method

The default implementation raises the [[EVENT_BEFORE_RESTORE]] event.
public beforeRestore ( ) : boolean
return boolean whether the record should be restored. Defaults to true.

beforeSoftDelete() public method

The default implementation raises the [[EVENT_BEFORE_SOFT_DELETE]] event.
public beforeSoftDelete ( ) : boolean
return boolean whether the record should be deleted. Defaults to true.

events() public method

public events ( )

getReplaceRegularDelete() public method

isDeleteAllowed() protected method

protected isDeleteAllowed ( ) : boolean
return boolean whether owner "hard" deletion allowed or not.

restore() public method

Restores record from "deleted" state, after it has been "soft" deleted.
public restore ( ) : integer | false
return integer | false the number of restored rows, or false if the restoration is unsuccessful for some reason.

restoreInternal() protected method

protected restoreInternal ( ) : integer
return integer the number of restored rows.

safeDelete() public method

If owner database supports transactions, regular deleting attempt will be enclosed in transaction with rollback in case of failure.
public safeDelete ( ) : false | integer
return false | integer number of affected rows.

setReplaceRegularDelete() public method

public setReplaceRegularDelete ( boolean $replaceRegularDelete )
$replaceRegularDelete boolean

softDelete() public method

Marks the owner as deleted.
public softDelete ( ) : integer | false
return integer | false the number of rows marked as deleted, or false if the soft deletion is unsuccessful for some reason. Note that it is possible the number of rows deleted is 0, even though the soft deletion execution is successful.

softDeleteInternal() protected method

Marks the owner as deleted.
protected softDeleteInternal ( ) : integer | false
return integer | false the number of rows marked as deleted, or false if the soft deletion is unsuccessful for some reason.

Property Details

$allowDeleteCallback public property

callback, which execution determines if record should be "hard" deleted instead of being marked as deleted. Callback should match following signature: boolean function(BaseActiveRecord $model) For example: php function ($user) { return $user->lastLoginDate === null; }
public $allowDeleteCallback

$deleteFallbackException public property

class name of the exception, which should trigger a fallback to SoftDeleteBehavior::softDelete method from SoftDeleteBehavior::safeDelete. By default [[\yii\db\IntegrityException]] is used, 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 [[\Exception]] will cause soft-delete fallback on any error during regular deleting.
See also: safeDelete()
public $deleteFallbackException

$invokeDeleteEvents public property

whether to invoke owner [[BaseActiveRecord::beforeDelete()]] and [[BaseActiveRecord::afterDelete()]] while performing soft delete. This option affects only SoftDeleteBehavior::softDelete method.
public $invokeDeleteEvents

$restoreAttributeValues public property

values of the owner attributes, which should be applied on restoration from "deleted" state, in format: [attributeName => attributeValue]. If not set value will be automatically detected from [[softDeleteAttributeValues]].
public $restoreAttributeValues

$softDeleteAttributeValues public property

values of the owner attributes, which should be applied on soft delete, in format: [attributeName => attributeValue]. Those may raise a flag: php ['isDeleted' => true] or switch status: php ['statusId' => Item::STATUS_DELETED] Attribute value can be a callable: php ['isDeleted' => function ($model) {return time()}]
public $softDeleteAttributeValues