# QueryBehavior

> Scopes SQL yii\db\ActiveQuery results to records that are, or are not, marked as deleted by yii2\extensions\softdelete\DeleteBehavior.

[API reference](/docs/api/index.md) › [Soft Delete](/docs/extensions/soft-delete/reference/index.md) › QueryBehavior

| class | `yii2\extensions\softdelete\QueryBehavior` |
| --- | --- |
| Extends | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Source | [`src/QueryBehavior.php`](https://github.com/yii2-extensions/soft-delete/blob/main/src/QueryBehavior.php#L27) |

Scopes SQL [`ActiveQuery`](/docs/api/yii/db/activequery/index.md) results to records that are, or are not, marked as deleted by [`DeleteBehavior`](/docs/extensions/soft-delete/reference/deletebehavior/index.md).

The query model must have a [`DeleteBehavior`](/docs/extensions/soft-delete/reference/deletebehavior/index.md) attached; the default conditions derive from its soft-delete and
restore values. Return a [`ScopedQuery`](/docs/extensions/soft-delete/reference/scopedquery/index.md), 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

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-deletedCondition"></span>`$deletedCondition` | `array<array-key, mixed>` | Condition that selects deleted records. | `QueryBehavior` |
| <span id="property-notDeletedCondition"></span>`$notDeletedCondition` | `array<array-key, mixed>` | Condition that selects records that are not deleted. | `QueryBehavior` |
| <span id="property-owner"></span>[`$owner`](/docs/api/yii/base/behavior/index.md#property-owner) | `yii\base\T\|null` | The owner of this behavior Default: `null`. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-attach"></span>[`attach()`](/docs/api/yii/base/behavior/index.md#method-attach) | Attaches the behavior object to the component. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`deleted()`](#method-deleted) | Restricts the owner query to deleted records, replacing any soft-delete scope applied before. | `QueryBehavior` |
| <span id="method-detach"></span>[`detach()`](/docs/api/yii/base/behavior/index.md#method-detach) | Detaches the behavior object from the component. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| <span id="method-events"></span>[`events()`](/docs/api/yii/base/behavior/index.md#method-events) | Declares event handlers for the [`$owner`](/docs/api/yii/base/behavior/index.md#property-owner)'s events. | [`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md) |
| [`filterDeleted()`](#method-filterDeleted) | Applies the soft-delete scope selected by a filter value. | `QueryBehavior` |
| [`getDeletedCondition()`](#method-getDeletedCondition) | Returns the condition that selects deleted records, building the default one on first use. | `QueryBehavior` |
| [`getNotDeletedCondition()`](#method-getNotDeletedCondition) | Returns the condition that selects records that are not deleted, building the default one on first use. | `QueryBehavior` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`notDeleted()`](#method-notDeleted) | Restricts the owner query to records that are not deleted, replacing any soft-delete scope applied before. | `QueryBehavior` |
| [`setDeletedCondition()`](#method-setDeletedCondition) | Sets the condition used to select soft-deleted records. | `QueryBehavior` |
| [`setNotDeletedCondition()`](#method-setNotDeletedCondition) | Sets the condition used to select records that are not soft-deleted. | `QueryBehavior` |
| [`withDeleted()`](#method-withDeleted) | Removes the soft-delete scope from the owner query and keeps its other conditions. | `QueryBehavior` |
| [`addFilterCondition()`](#method-addFilterCondition) protected | Replaces the soft-delete scope of the owner query with the given condition. | `QueryBehavior` |
| [`defaultDeletedCondition()`](#method-defaultDeletedCondition) protected | Builds the condition that matches the model's soft-delete values. | `QueryBehavior` |
| [`defaultNotDeletedCondition()`](#method-defaultNotDeletedCondition) protected | Builds the condition that matches the model's restore values without invoking any computed value. | `QueryBehavior` |
| [`getModelInstance()`](#method-getModelInstance) protected | Returns the model instance of the owner query. | `QueryBehavior` |
| [`normalizeFilterCondition()`](#method-normalizeFilterCondition) protected | Wraps the condition as a soft-delete scope qualified with the current primary table alias. | `QueryBehavior` |

## Method details

### deleted() <span id="method-deleted"></span>

```php
public function deleted(): yii\db\ActiveQuery
```

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

**Returns**

[`yii\db\ActiveQuery`](/docs/api/yii/db/activequery/index.md): The owner query.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the default condition cannot be built for the query model.

### filterDeleted() <span id="method-filterDeleted"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$deleted` | `mixed` |  |

**Returns**

[`yii\db\ActiveQuery`](/docs/api/yii/db/activequery/index.md): The owner query.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): When the value is not one of the accepted values.
- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the default condition cannot be built for the query model.

### getDeletedCondition() <span id="method-getDeletedCondition"></span>

```php
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`](/docs/api/yii/base/invalidconfigexception/index.md): When the default condition cannot be built for the query model.

### getNotDeletedCondition() <span id="method-getNotDeletedCondition"></span>

```php
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`](/docs/api/yii/base/invalidconfigexception/index.md): When the default condition cannot be built for the query model.

### notDeleted() <span id="method-notDeleted"></span>

```php
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`](/docs/api/yii/db/activequery/index.md): The owner query.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the default condition cannot be built for the query model.

### setDeletedCondition() <span id="method-setDeletedCondition"></span>

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

Sets the condition used to select soft-deleted records.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$deletedCondition` | `array<array-key, mixed>\|null` | Condition that selects deleted records, or `null` to use the default one. |

### setNotDeletedCondition() <span id="method-setNotDeletedCondition"></span>

```php
public function setNotDeletedCondition(array<array-key, mixed>|null $notDeletedCondition): void
```

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

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$notDeletedCondition` | `array<array-key, mixed>\|null` | Condition that selects records that are not deleted, or `null` to use the default one. |

### withDeleted() <span id="method-withDeleted"></span>

```php
public function withDeleted(): yii\db\ActiveQuery
```

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

**Returns**

[`yii\db\ActiveQuery`](/docs/api/yii/db/activequery/index.md)

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the behavior is not attached to a SQL ActiveQuery.

### addFilterCondition() <span id="method-addFilterCondition"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$condition` | `array<array-key, mixed>` | Condition that selects the scoped records. |

**Returns**

[`yii\db\ActiveQuery`](/docs/api/yii/db/activequery/index.md): The owner query.

### defaultDeletedCondition() <span id="method-defaultDeletedCondition"></span>

```php
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`](/docs/api/yii/base/invalidconfigexception/index.md): When the query model has no [`DeleteBehavior`](/docs/extensions/soft-delete/reference/deletebehavior/index.md), or a computed value has a restore value other than `null`.

### defaultNotDeletedCondition() <span id="method-defaultNotDeletedCondition"></span>

```php
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`](/docs/api/yii/base/invalidconfigexception/index.md): When the query model has no [`DeleteBehavior`](/docs/extensions/soft-delete/reference/deletebehavior/index.md), its restore values cannot be inferred, or one of them is computed.

### getModelInstance() <span id="method-getModelInstance"></span>

```php
protected function getModelInstance(): yii\db\BaseActiveRecord
```

Returns the model instance of the owner query.

**Returns**

[`yii\db\BaseActiveRecord`](/docs/api/yii/db/baseactiverecord/index.md)

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): When the query model is not an ActiveRecord.

### normalizeFilterCondition() <span id="method-normalizeFilterCondition"></span>

```php
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**

| Parameter | Type | Description |
| --- | --- | --- |
| `$condition` | `array<array-key, mixed>` | Condition that selects the scoped records. |

**Returns**

[`Condition`](/docs/extensions/soft-delete/reference/condition/index.md)