PHP Class SoftDeleteBehavior

Uso: deve existir uma coluna na tabela do modelo utilizado que indique se o registro está ativo ou não (valor binário do tipo boolean, integer, tinyint, etc). Por default o nome dessa coluna é 'active'. Se outro nome for utilizado, deve ser informado ao behavior através do atributo 'field' das configurações do behavior. A exclusão de registros é feita através do método softDelete(). Qualquer busca feita pelos registros que utilizem o behavior SoftDelete somente incluem os registros cujo campo active seja true (1) PHP version > 5.3.1 Licensed under The MIT License Redistributions of files must retain the above copyright notice.
Inheritance: extends ModelBehavior
Show file Open project: baserproject/basercms

Public Properties

Property Type Description
$default array Default settings
$runtime array Holds activity flags for models

Public Methods

Method Description
beforeDelete ( Model $model, boolean $cascade = true ) : boolean Before delete callback
beforeFind ( Model $model, array $query ) : array Before find callback
delete ( object $model, integer $id ) : boolean Mark record as deleted
existsAndNotDeleted ( Model $model, $id ) : mixed Check if a record exists for the given id
purgeDeleted ( object $model, mixed $expiration = '-90 days' ) : boolean Purge table
purgeDeletedCount ( object $model, mixed $expiration = '-90 days' ) : integer Returns number of outdated softdeleted records prepared for purge
setup ( Model $model, array $settings = [] ) Setup callback
softDelete ( object $model, mixed $active ) : mixed Enable/disable SoftDelete functionality
undelete ( object $model, integer $id ) : boolean Mark record as not deleted

Protected Methods

Method Description
_normalizeFields ( object $model, array $settings = [] ) : array Return normalized field array
_purgeDeletedConditions ( object $model, mixed $expiration = '-90 days' ) : array Returns conditions for finding outdated records
_softDeleteAssociations ( object $model, mixed $active ) Modifies conditions of hasOne and hasMany associations

Method Details

_normalizeFields() protected method

Return normalized field array
protected _normalizeFields ( object $model, array $settings = [] ) : array
$model object
$settings array
return array

_purgeDeletedConditions() protected method

Returns conditions for finding outdated records
protected _purgeDeletedConditions ( object $model, mixed $expiration = '-90 days' ) : array
$model object
$expiration mixed anything parseable by strtotime(), by default '-90 days'
return array

_softDeleteAssociations() protected method

If multiple delete flags are configured for model, then $active=true doesn't do anything - you have to alter conditions in association definition
protected _softDeleteAssociations ( object $model, mixed $active )
$model object
$active mixed

beforeDelete() public method

Before delete callback
public beforeDelete ( Model $model, boolean $cascade = true ) : boolean
$model Model
$cascade boolean
return boolean

beforeFind() public method

Before find callback
public beforeFind ( Model $model, array $query ) : array
$model Model
$query array
return array

delete() public method

Mark record as deleted
public delete ( object $model, integer $id ) : boolean
$model object
$id integer
return boolean

existsAndNotDeleted() public method

Check if a record exists for the given id
public existsAndNotDeleted ( Model $model, $id ) : mixed
$model Model
return mixed

purgeDeleted() public method

Purge table
public purgeDeleted ( object $model, mixed $expiration = '-90 days' ) : boolean
$model object
$expiration mixed anything parseable by strtotime(), by default '-90 days'
return boolean if there were some outdated records

purgeDeletedCount() public method

Returns number of outdated softdeleted records prepared for purge
public purgeDeletedCount ( object $model, mixed $expiration = '-90 days' ) : integer
$model object
$expiration mixed anything parseable by strtotime(), by default '-90 days'
return integer

setup() public method

Setup callback
public setup ( Model $model, array $settings = [] )
$model Model
$settings array

softDelete() public method

Usage from model: $this->softDelete(false); deactivate this behavior for model $this->softDelete('field_two'); enabled only for this flag field $this->softDelete(true); enable again for all flag fields $config = $this->softDelete(null); for obtaining current setting
public softDelete ( object $model, mixed $active ) : mixed
$model object
$active mixed
return mixed if $active is null, then current setting/null, or boolean if runtime setting for model was changed

undelete() public method

Mark record as not deleted
public undelete ( object $model, integer $id ) : boolean
$model object
$id integer
return boolean

Property Details

$default public property

Default settings
public array $default
return array

$runtime public property

Holds activity flags for models
public array $runtime
return array