Skip to content
PHP Ecosystem
Explore

ScopedQuery

API reference › Soft Delete › ScopedQuery

SQL ActiveQuery that retains its soft-delete scope when alias() changes the primary table alias.

from() does not re-qualify the scope: rename the primary table with alias(), or call from() before choosing the scope.

PropertyTypeDescriptionDefined by
$asArraybool|nullWhether to return each record as an array. Default: null.yii\db\ActiveQueryTrait
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$distinctboolWhether to select distinct rows of data only. Default: false.yii\db\Query
$emulateExecutionboolWhether to emulate the actual query execution, returning empty or false results. Default: false. Available since 2.0.11.yii\db\QueryTrait
$fromarray|string|nullThe table(s) to be selected from. Default: null.yii\db\Query
$groupByarray|nullHow to group the query results. Default: null.yii\db\Query
$havingstring|array|yii\db\ExpressionInterface|nullThe condition to be applied in the GROUP BY clause. Default: null.yii\db\Query
$indexBystring|callable|nullThe name of the column by which the query results should be indexed by. Default: null.yii\db\QueryTrait
$inverseOfstring|nullThe name of the relation that is the inverse of this relation. Default: null.yii\db\ActiveRelationTrait
$joinarray|nullHow to join with other tables. Default: null.yii\db\Query
$joinWitharray|nullA list of relations that this query should be joined with Default: null.yii\db\ActiveQuery
$limitint|yii\db\ExpressionInterface|nullMaximum number of records to be returned. Default: null.yii\db\QueryTrait
$linkarrayThe columns of the primary and foreign tables that establish a relation. Default: null.yii\db\ActiveRelationTrait
$modelClassclass-string<yii\db\ActiveRecordInterface>The name of the ActiveRecord class. Default: null.yii\db\ActiveQueryTrait
$multipleboolWhether this query represents a relation to more than one record. Default: null.yii\db\ActiveRelationTrait
$offsetint|yii\db\ExpressionInterface|nullZero-based offset from where the records are to be returned. Default: null.yii\db\QueryTrait
$onstring|array|nullThe join condition to be used when this query is used in a relational context. Default: null.yii\db\ActiveQuery
$orderByarray|nullHow to sort the query results. Default: null.yii\db\QueryTrait
$paramsarray|nullList of query parameter values indexed by parameter placeholders. Default: [].yii\db\Query
$primaryModelyii\db\BaseActiveRecord|nullThe primary model of a relational query. Default: null.yii\db\ActiveRelationTrait
$queryCacheDependencyyii\caching\Dependency|nullThe dependency to be associated with the cached query result for this query Default: null. Available since 2.0.14.yii\db\Query
$queryCacheDurationint|bool|nullThe default number of seconds that query results can remain valid in cache. Default: null. Available since 2.0.14.yii\db\Query
$selectarray|nullThe columns being selected. Default: null.yii\db\Query
$selectOptionstring|nullAdditional option that should be appended to the ‘SELECT’ keyword. Default: null.yii\db\Query
$sqlstring|nullThe SQL statement to be executed for retrieving AR records. Default: null.yii\db\ActiveQuery
$tablesUsedInFrom read-onlystring[]Table names indexed by aliases.yii\db\Query
$unionarray|nullThis is used to construct the UNION clause(s) in a SQL statement. Default: null.yii\db\Query
$unionLimitint|yii\db\ExpressionInterface|nullNumber of records to return from the complete UNION result. Default: null. Available since 22.0.yii\db\Query
$unionOffsetint|yii\db\ExpressionInterface|nullZero-based offset for the complete UNION result. Default: null. Available since 22.0.yii\db\Query
$unionOrderByarray<int|string, int|yii\db\ExpressionInterface>|nullHow to sort the complete UNION result. Default: null. Available since 22.0.yii\db\Query
$viaarray|object|nullThe query associated with the junction table. Default: null.yii\db\ActiveRelationTrait
$wherestring|array|yii\db\ExpressionInterface|nullQuery condition. Default: null.yii\db\QueryTrait
$witharray|nullA list of relations that this query should be performed with Default: null.yii\db\ActiveQueryTrait
$withQueriesarray|nullThis is used to construct the WITH section in a SQL query. Default: null. Available since 2.0.35.yii\db\Query
MethodDescriptionDefined by
__construct()Constructor.yii\db\ActiveQuery
__call()Calls the named method which is not a class method.yii\base\Component
__clone()Keeps the softDelete behavior of the cloned query, with its configured conditions, on the clone.ScopedQuery
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__toString()Returns the SQL representation of Queryyii\db\Query
__unset()Sets a component property to be null.yii\base\Component
addGroupBy()Adds additional group-by columns to the existing ones.yii\db\Query
addOrderBy()Adds additional ORDER BY columns to the query.yii\db\QueryTrait
addParams()Adds additional parameters to be bound to the query.yii\db\Query
addSelect()Add more columns to the SELECT part of the query.yii\db\Query
addUnionOrderBy()Adds ORDER BY columns to the complete UNION result.yii\db\Query
alias()Sets the primary table alias and re-qualifies the soft-delete scope with it.ScopedQuery
all()Executes query and returns all results as an array.yii\db\ActiveQuery
andFilterCompare()Adds a filtering condition for a specific column and allow the user to choose a filter operator.yii\db\Query
andFilterHaving()Adds an additional HAVING condition to the existing one but ignores empty operands.yii\db\Query
andFilterWhere()Adds an additional WHERE condition to the existing one but ignores empty operands.yii\db\QueryTrait
andHaving()Adds an additional HAVING condition to the existing one.yii\db\Query
andOnCondition()Adds an additional ON condition to the existing one.yii\db\ActiveQuery
andWhere()Adds an additional WHERE condition to the existing one.yii\db\Query
asArray()Sets the asArray() property.yii\db\ActiveQuery
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
average()Returns the average of the specified column values.yii\db\Query
batch()The batch query result.yii\db\ActiveQuery
behaviors()Declares QueryBehavior as the softDelete behavior.ScopedQuery
cache()Enables query cache for this Query.yii\db\Query
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
column()Executes the query and returns the first column of the result.yii\db\Query
count()Returns the number of records.yii\db\Query
create() staticCreates a new Query object and copies its property values from an existing one.yii\db\Query
createCommand()Creates a DB command that can be used to execute this query.yii\db\ActiveQuery
deleted()ScopedQuery
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
distinct()Sets the value indicating whether to SELECT DISTINCT or not.yii\db\Query
each()The batch query result.yii\db\ActiveQuery
emulateExecution()Sets whether to emulate query execution, preventing any interaction with data storage.yii\db\QueryTrait
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
exists()Returns a value indicating whether the query result contains any row of data.yii\db\Query
filterDeleted()ScopedQuery
filterHaving()Sets the HAVING part of the query but ignores empty operands.yii\db\Query
filterWhere()Sets the WHERE part of the query but ignores empty operands.yii\db\QueryTrait
findFor()Finds the related records for the specified primary record.yii\db\ActiveRelationTrait
findWith()Finds records corresponding to one or multiple relations and populates them into the primary models.yii\db\ActiveQueryTrait
from()Sets the FROM part of the query.yii\db\Query
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
getTablesUsedInFrom()Returns table names used in from() indexed by aliases.yii\db\ActiveQuery
groupBy()Sets the GROUP BY part of the query.yii\db\Query
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.yii\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
having()Sets the HAVING part of the query.yii\db\Query
indexBy()Sets the indexBy() property.yii\db\QueryTrait
init()Initializes the object.yii\db\ActiveQuery
innerJoin()Appends an INNER JOIN part to the query.yii\db\Query
innerJoinWith()Inner joins with the specified relations.yii\db\ActiveQuery
inverseOf()Sets the name of the relation that is the inverse of this relation.yii\db\ActiveRelationTrait
join()Appends a JOIN part to the query.yii\db\Query
joinWith()Joins with the specified relations.yii\db\ActiveQuery
leftJoin()Appends a LEFT OUTER JOIN part to the query.yii\db\Query
limit()Sets the LIMIT part of the query.yii\db\QueryTrait
max()Returns the maximum of the specified column values.yii\db\Query
min()Returns the minimum of the specified column values.yii\db\Query
noCache()Disables query cache for this Query.yii\db\Query
notDeleted()ScopedQuery
off()Detaches an existing event handler from this component.yii\base\Component
offset()Sets the OFFSET part of the query.yii\db\QueryTrait
on()Attaches an event handler to an event.yii\base\Component
onCondition()Sets the ON condition for a relational query.yii\db\ActiveQuery
one()Executes query and returns a single row of result.yii\db\ActiveQuery
orderBy()Sets the ORDER BY part of the query.yii\db\QueryTrait
orFilterHaving()Adds an additional HAVING condition to the existing one but ignores empty operands.yii\db\Query
orFilterWhere()Adds an additional WHERE condition to the existing one but ignores empty operands.yii\db\QueryTrait
orHaving()Adds an additional HAVING condition to the existing one.yii\db\Query
orOnCondition()Adds an additional ON condition to the existing one.yii\db\ActiveQuery
orWhere()Adds an additional WHERE condition to the existing one.yii\db\Query
params()Sets the parameters to be bound to the query.yii\db\Query
populate()Converts the raw query results into the format as specified by this query.yii\db\ActiveQuery
populateRelation()Finds the related records and populates them into the primary models.yii\db\ActiveRelationTrait
prepare()Prepares for building SQL.yii\db\ActiveQuery
rightJoin()Appends a RIGHT OUTER JOIN part to the query.yii\db\Query
scalar()Returns the query result as a scalar value.yii\db\Query
select()Sets the SELECT part of the query.yii\db\Query
sum()Returns the sum of the specified column values.yii\db\Query
trigger()Triggers an event.yii\base\Component
union()Appends a SQL statement using UNION operator.yii\db\Query
unionLimit()Sets the LIMIT part of the complete UNION result.yii\db\Query
unionOffset()Sets the OFFSET part of the complete UNION result.yii\db\Query
unionOrderBy()Sets the ORDER BY part of the complete UNION result.yii\db\Query
via()Specifies the relation associated with the junction table.yii\db\ActiveRelationTrait
viaTable()Specifies the junction table for a relational query.yii\db\ActiveQuery
where()Sets the WHERE part of the query.yii\db\Query
with()Specifies the relations with which this query should be performed.yii\db\ActiveQueryTrait
withDeleted()ScopedQuery
withQuery()Prepends a SQL statement using WITH syntax.yii\db\Query
cleanUpTableNames() protectedClean up table names and aliases Both aliases and names are enclosed into {{ and }}.yii\db\Query
createModels() protectedConverts found rows into model instances.yii\db\ActiveQueryTrait
filterCondition() protectedRemoves empty operands from the given query condition.yii\db\QueryTrait
getPrimaryTableName() protectedyii\db\ActiveQuery
getTableNameAndAlias() protectedReturns the table name and the table alias for $modelClass.yii\db\ActiveQuery
isEmpty() protectedReturns a value indicating whether the give value is “empty”.yii\db\QueryTrait
normalizeOrderBy() protectedNormalizes format of ORDER BY data.yii\db\QueryTrait
normalizeSelect() protectedNormalizes the SELECT columns passed to select() or addSelect().yii\db\Query
queryScalar() protectedQueries a scalar value by setting select() first.yii\db\ActiveQuery
setCommandCache() protectedSets $command cache, if this query has enabled caching.yii\db\Query
EventTypeDescriptionDefined by
EVENT_INITyii\db\EventAn event that is triggered when the query is initialized via init(). Event name: 'init'.yii\db\ActiveQuery
public function __clone(): mixed

Keeps the softDelete behavior of the cloned query, with its configured conditions, on the clone.

Yii detaches every behavior from a cloned component, and behaviors() alone would declare the default one.

Returns

mixed

public function alias(string $alias): static

Sets the primary table alias and re-qualifies the soft-delete scope with it.

Parameters

ParameterTypeDescription
$aliasstringTable alias, with or without {{ and }} quoting.

Returns

static

public function behaviors(): array<string, class-string<yii2\extensions\softdelete\QueryBehavior>>

Declares QueryBehavior as the softDelete behavior.

Returns

array<string, class-string<QueryBehavior>>

public function deleted(): $this

Returns

$this

public function filterDeleted(mixed $deleted): $this

Parameters

ParameterTypeDescription
$deletedmixed

Returns

$this

public function notDeleted(): $this

Returns

$this

public function withDeleted(): $this

Returns

$this