QueryBehavior
API reference › Soft Delete › QueryBehavior
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.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$deletedCondition | array<array-key, | Condition that selects deleted records. | QueryBehavior |
$notDeletedCondition | array<array-key, | Condition that selects records that are not deleted. | QueryBehavior |
$owner | yii\ | The owner of this behavior Default: null. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
attach() | Attaches the behavior object to the component. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
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\ |
events() | Declares event handlers for the $owner’s events. | yii\ |
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\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
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() protected | Replaces the soft-delete scope of the owner query with the given condition. | QueryBehavior |
defaultDeletedCondition() protected | Builds the condition that matches the model’s soft-delete values. | QueryBehavior |
defaultNotDeletedCondition() protected | Builds the condition that matches the model’s restore values without invoking any computed value. | QueryBehavior |
getModelInstance() protected | Returns the model instance of the owner query. | QueryBehavior |
normalizeFilterCondition() protected | Wraps the condition as a soft-delete scope qualified with the current primary table alias. | QueryBehavior |
Method details
Section titled “Method details”deleted()
Section titled “deleted() ”public function deleted(): yii\db\ActiveQueryRestricts the owner query to deleted records, replacing any soft-delete scope applied before.
Returns
yii\db\ActiveQuery: The owner query.
Throws
yii\base\InvalidConfigException: When the default condition cannot be built for the query model.
filterDeleted()
Section titled “filterDeleted() ”public function filterDeleted(mixed $deleted): yii\db\ActiveQueryApplies 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
| Parameter | Type | Description |
|---|---|---|
$deleted | mixed |
Returns
yii\db\ActiveQuery: The owner query.
Throws
InvalidArgumentException: When the value is not one of the accepted values.yii\base\InvalidConfigException: When the default condition cannot be built for the query model.
getDeletedCondition()
Section titled “getDeletedCondition() ”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
yii\base\InvalidConfigException: When the default condition cannot be built for the query model.
getNotDeletedCondition()
Section titled “getNotDeletedCondition() ”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
yii\base\InvalidConfigException: When the default condition cannot be built for the query model.
notDeleted()
Section titled “notDeleted() ”public function notDeleted(): yii\db\ActiveQueryRestricts the owner query to records that are not deleted, replacing any soft-delete scope applied before.
Returns
yii\db\ActiveQuery: The owner query.
Throws
yii\base\InvalidConfigException: When the default condition cannot be built for the query model.
setDeletedCondition()
Section titled “setDeletedCondition() ”public function setDeletedCondition(array<array-key, mixed>|null $deletedCondition): voidSets the condition used to select soft-deleted records.
Parameters
| Parameter | Type | Description |
|---|---|---|
$deletedCondition | array<array-key, | Condition that selects deleted records, or null to use the default one. |
setNotDeletedCondition()
Section titled “setNotDeletedCondition() ”public function setNotDeletedCondition(array<array-key, mixed>|null $notDeletedCondition): voidSets the condition used to select records that are not soft-deleted.
Parameters
| Parameter | Type | Description |
|---|---|---|
$notDeletedCondition | array<array-key, | Condition that selects records that are not deleted, or null to use the default one. |
withDeleted()
Section titled “withDeleted() ”public function withDeleted(): yii\db\ActiveQueryRemoves the soft-delete scope from the owner query and keeps its other conditions.
Returns
Throws
yii\base\InvalidConfigException: When the behavior is not attached to a SQL ActiveQuery.
addFilterCondition()
Section titled “addFilterCondition() ”protected function addFilterCondition(array<array-key, mixed> $condition): yii\db\ActiveQueryReplaces the soft-delete scope of the owner query with the given condition.
Parameters
| Parameter | Type | Description |
|---|---|---|
$condition | array<array-key, | Condition that selects the scoped records. |
Returns
yii\db\ActiveQuery: The owner query.
defaultDeletedCondition()
Section titled “defaultDeletedCondition() ”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
yii\base\InvalidConfigException: When the query model has noDeleteBehavior, or a computed value has a restore value other thannull.
defaultNotDeletedCondition()
Section titled “defaultNotDeletedCondition() ”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
yii\base\InvalidConfigException: When the query model has noDeleteBehavior, its restore values cannot be inferred, or one of them is computed.
getModelInstance()
Section titled “getModelInstance() ”protected function getModelInstance(): yii\db\BaseActiveRecordReturns the model instance of the owner query.
normalizeFilterCondition()
Section titled “normalizeFilterCondition() ”protected function normalizeFilterCondition(array<array-key, mixed> $condition): yii2\extensions\softdelete\ConditionWraps the condition as a soft-delete scope qualified with the current primary table alias.
Parameters
| Parameter | Type | Description |
|---|---|---|
$condition | array<array-key, | Condition that selects the scoped records. |
Returns