phpEZ v0.2.0

SoftDelete

Trait adding soft-delete support to a Model.

Instead of removing rows, delete() (by default) sets a deleted_at timestamp. find()/findMany() transparently exclude soft-deleted rows unless a SoftDeleteFilter is given.

Usage:

class Post extends Model {
  use SoftDelete;
}

$post->delete();                       // soft delete
$post->delete(soft: false);            // hard delete
$post->restore();                      // undo soft delete
Post::find($id, softDeleteFilter: SoftDeleteFilter::ONLY);
Tags
subpackage

db

Table of Contents

Properties

$deleted_at  : DBDateTime|null

Methods

delete()  : void
Delete this model instance (soft delete by default).
find()  : static|null
Find a single model with a given condition.
findMany()  : array<string|int, mixed>
Find multiple models with a given condition.
id()  : int|null
isDeleted()  : bool
Check whether this instance is soft-deleted.
restore()  : static
Undo a soft delete by clearing deleted_at and saving.
save()  : static
softDeleteFilter()  : string|null
Build the default soft-delete filter condition.

Properties

$deleted_at

protected DBDateTime|null $deleted_at = \null
Attributes
#[Index]
'idx_deleted_at'

Methods

delete()

Delete this model instance (soft delete by default).

public delete([bool $soft = true ]) : void
Parameters
$soft : bool = true

If true, performs a soft delete (default). If false, performs a hard delete.

find()

Find a single model with a given condition.

public static find(string $id_or_val[, string $field = 'id' ][, SoftDeleteFilter $softDeleteFilter = SoftDeleteFilter::EXCLUDE ]) : static|null
Parameters
$id_or_val : string

The value to search for.

$field : string = 'id'

The field to search in.

$softDeleteFilter : SoftDeleteFilter = SoftDeleteFilter::EXCLUDE

The soft delete filter to apply.

Return values
static|null

The found model, or null if not found.

findMany()

Find multiple models with a given condition.

public static findMany([string $cond = '1=1' ][, array<string|int, mixed> $fieldSet = [] ][, string $joins = '' ][, SoftDeleteFilter $filter = SoftDeleteFilter::EXCLUDE ]) : array<string|int, mixed>
Parameters
$cond : string = '1=1'

The condition to apply.

$fieldSet : array<string|int, mixed> = []

The fields to include in the query.

$joins : string = ''

The joins to include in the query.

$filter : SoftDeleteFilter = SoftDeleteFilter::EXCLUDE

The soft delete filter to apply.

Return values
array<string|int, mixed>

The found models.

id()

public abstract id() : int|null
Return values
int|null

isDeleted()

Check whether this instance is soft-deleted.

public isDeleted() : bool
Return values
bool

True if deleted_at is set.

restore()

Undo a soft delete by clearing deleted_at and saving.

public restore() : static
Tags
throws
DataException

If the instance has no ID or is not currently deleted.

Return values
static

Returns $this for fluent interface.

save()

public abstract save([mixed $forCreate = false ][, int|null $idForUpdate = null ]) : static
Parameters
$forCreate : mixed = false
$idForUpdate : int|null = null
Return values
static

softDeleteFilter()

Build the default soft-delete filter condition.

protected static softDeleteFilter(SoftDeleteFilter $filter) : string|null
Parameters
$filter : SoftDeleteFilter

The filter to apply.

Return values
string|null

SQL condition to append, or null when no filter is needed.

On this page

Search results