Skip to content
PHP Ecosystem
Explore

QueryBehavior

API reference › Soft Delete › QueryBehavior

classyii2\extensions\softdelete\QueryBehavior
Extendsyii\base\Behavior » yii\base\BaseObject
Implementsyii\base\Configurable
Sourcesrc/QueryBehavior.php

Scopes SQL ActiveQuery results to records that are, or are not, marked as deleted by DeleteBehavior.

The query model must have a DeleteBehavior attached; the default conditions derive from its soft-delete and restore values. Return a ScopedQuery, or a subclass of it, from find(): it declares this behavior, so the behavior survives query cloning, and it re-qualifies the scope when the table alias changes.

PropertyTypeDescriptionDefined by
$deletedConditionarray<array-key, mixed>Condition that selects deleted records.QueryBehavior
$notDeletedConditionarray<array-key, mixed>Condition that selects records that are not deleted.QueryBehavior
$owneryii\base\T|nullThe owner of this behavior Default: null.yii\base\Behavior
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
attach()Attaches the behavior object to the component.yii\base\Behavior
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
deleted()Restricts the owner query to deleted records, replacing any soft-delete scope applied before.QueryBehavior
detach()Detaches the behavior object from the component.yii\base\Behavior
events()Declares event handlers for the $owner’s events.yii\base\Behavior
filterDeleted()Applies the soft-delete scope selected by a filter value.QueryBehavior
getDeletedCondition()Returns the condition that selects deleted records, building the default one on first use.QueryBehavior
getNotDeletedCondition()Returns the condition that selects records that are not deleted, building the default one on first use.QueryBehavior
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
notDeleted()Restricts the owner query to records that are not deleted, replacing any soft-delete scope applied before.QueryBehavior
setDeletedCondition()Sets the condition used to select soft-deleted records.QueryBehavior
setNotDeletedCondition()Sets the condition used to select records that are not soft-deleted.QueryBehavior
withDeleted()Removes the soft-delete scope from the owner query and keeps its other conditions.QueryBehavior
addFilterCondition() protectedReplaces the soft-delete scope of the owner query with the given condition.QueryBehavior
defaultDeletedCondition() protectedBuilds the condition that matches the model’s soft-delete values.QueryBehavior
defaultNotDeletedCondition() protectedBuilds the condition that matches the model’s restore values without invoking any computed value.QueryBehavior
getModelInstance() protectedReturns the model instance of the owner query.QueryBehavior
normalizeFilterCondition() protectedWraps the condition as a soft-delete scope qualified with the current primary table alias.QueryBehavior
public function deleted(): yii\db\ActiveQuery

Restricts the owner query to deleted records, replacing any soft-delete scope applied before.

Returns

yii\db\ActiveQuery: The owner query.

Throws

public function filterDeleted(mixed $deleted): yii\db\ActiveQuery

Applies the soft-delete scope selected by a filter value.

The values '', null and [] select records that are not deleted; true, 1 and '1' select deleted records; false, 0, '0' and 'all' select all records. Values are compared strictly, so validate request input before passing it.

Parameters

ParameterTypeDescription
$deletedmixed

Returns

yii\db\ActiveQuery: The owner query.

Throws

public function getDeletedCondition(): array<array-key, mixed>

Returns the condition that selects deleted records, building the default one on first use.

Returns

array<array-key, mixed>: Condition that selects deleted records.

Throws

public function getNotDeletedCondition(): array<array-key, mixed>

Returns the condition that selects records that are not deleted, building the default one on first use.

Returns

array<array-key, mixed>: Condition that selects records that are not deleted.

Throws

public function notDeleted(): yii\db\ActiveQuery

Restricts the owner query to records that are not deleted, replacing any soft-delete scope applied before.

Returns

yii\db\ActiveQuery: The owner query.

Throws

public function setDeletedCondition(array<array-key, mixed>|null $deletedCondition): void

Sets the condition used to select soft-deleted records.

Parameters

ParameterTypeDescription
$deletedConditionarray<array-key, mixed>|nullCondition that selects deleted records, or null to use the default one.
public function setNotDeletedCondition(array<array-key, mixed>|null $notDeletedCondition): void

Sets the condition used to select records that are not soft-deleted.

Parameters

ParameterTypeDescription
$notDeletedConditionarray<array-key, mixed>|nullCondition that selects records that are not deleted, or null to use the default one.
public function withDeleted(): yii\db\ActiveQuery

Removes the soft-delete scope from the owner query and keeps its other conditions.

Returns

yii\db\ActiveQuery

Throws

protected function addFilterCondition(array<array-key, mixed> $condition): yii\db\ActiveQuery

Replaces the soft-delete scope of the owner query with the given condition.

Parameters

ParameterTypeDescription
$conditionarray<array-key, mixed>Condition that selects the scoped records.

Returns

yii\db\ActiveQuery: The owner query.

protected function defaultDeletedCondition(): array<array-key, mixed>

Builds the condition that matches the model’s soft-delete values.

A computed value cannot be matched, so it becomes a NOT NULL check, which requires its restore value to be null.

Returns

array<array-key, mixed>

Throws

protected function defaultNotDeletedCondition(): array<array-key, mixed>

Builds the condition that matches the model’s restore values without invoking any computed value.

Returns

array<array-key, mixed>

Throws

protected function getModelInstance(): yii\db\BaseActiveRecord

Returns the model instance of the owner query.

Returns

yii\db\BaseActiveRecord

Throws

protected function normalizeFilterCondition(array<array-key, mixed> $condition): yii2\extensions\softdelete\Condition

Wraps the condition as a soft-delete scope qualified with the current primary table alias.

Parameters

ParameterTypeDescription
$conditionarray<array-key, mixed>Condition that selects the scoped records.

Returns

Condition