PHP Класс yii2tech\ar\softdelete\SoftDeleteBehavior

php class Item extends ActiveRecord { public function behaviors() { return [ 'softDeleteBehavior' => [ 'class' => SoftDeleteBehavior::className(), 'softDeleteAttributeValues' => [ 'isDeleted' => true ], ], ]; } }
С версии: 1.0
Автор: Paul Klimov ([email protected])
Наследование: extends yii\base\Behavior
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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()}]

Открытые методы

Метод Описание
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.

Защищенные методы

Метод Описание
isDeleteAllowed ( ) : boolean
restoreInternal ( ) : integer
softDeleteInternal ( ) : integer | false Marks the owner as deleted.

Приватные методы

Метод Описание
beginTransaction ( ) : Transaction | null Begins new database transaction if owner allows it.

Описание методов

afterRestore() публичный Метод

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() публичный Метод

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() публичный Метод

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

beforeRestore() публичный Метод

The default implementation raises the [[EVENT_BEFORE_RESTORE]] event.
public beforeRestore ( ) : boolean
Результат boolean whether the record should be restored. Defaults to true.

beforeSoftDelete() публичный Метод

The default implementation raises the [[EVENT_BEFORE_SOFT_DELETE]] event.
public beforeSoftDelete ( ) : boolean
Результат boolean whether the record should be deleted. Defaults to true.

events() публичный Метод

public events ( )

getReplaceRegularDelete() публичный Метод

public getReplaceRegularDelete ( ) : boolean
Результат boolean

isDeleteAllowed() защищенный Метод

protected isDeleteAllowed ( ) : boolean
Результат boolean whether owner "hard" deletion allowed or not.

restore() публичный Метод

Restores record from "deleted" state, after it has been "soft" deleted.
public restore ( ) : integer | false
Результат integer | false the number of restored rows, or false if the restoration is unsuccessful for some reason.

restoreInternal() защищенный Метод

protected restoreInternal ( ) : integer
Результат integer the number of restored rows.

safeDelete() публичный Метод

If owner database supports transactions, regular deleting attempt will be enclosed in transaction with rollback in case of failure.
public safeDelete ( ) : false | integer
Результат false | integer number of affected rows.

setReplaceRegularDelete() публичный Метод

public setReplaceRegularDelete ( boolean $replaceRegularDelete )
$replaceRegularDelete boolean

softDelete() публичный Метод

Marks the owner as deleted.
public softDelete ( ) : integer | false
Результат 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() защищенный Метод

Marks the owner as deleted.
protected softDeleteInternal ( ) : integer | false
Результат integer | false the number of rows marked as deleted, or false if the soft deletion is unsuccessful for some reason.

Описание свойств

$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; }
public $allowDeleteCallback

$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.
См. также: safeDelete()
public $deleteFallbackException

$invokeDeleteEvents публичное свойство

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

$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]].
public $restoreAttributeValues

$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 $softDeleteAttributeValues