ElementQueryInterface

Type
Interface
Namespace
craft\elements\db
Extends
ArrayAccess, Countable, IteratorAggregate, yii\base\Arrayable, yii\db\QueryInterface
Implemented by
craft\elements\db\AssetQuery, craft\elements\db\CategoryQuery, craft\elements\db\ElementQuery, craft\elements\db\EntryQuery, craft\elements\db\GlobalSetQuery, craft\elements\db\MatrixBlockQuery, craft\elements\db\TagQuery, craft\elements\db\UserQuery
Since
3.0

ElementQueryInterface defines the common interface to be implemented by element query classes.

The default implementation of this interface is provided by craft\elements\db\ElementQuery.

View source

Public Methods

MethodDescription
addOrderBy()Adds additional ORDER BY columns to the query.
all()Executes the query and returns all results as an array.
ancestorDist()Narrows the query results to only {elements} that are up to a certain distance away from the {element} specified by ancestorOf().
ancestorOf()Narrows the query results to only {elements} that are ancestors of another {element}.
andFilterWhere()Adds an additional WHERE condition to the existing one ignoring empty parameters.
andWhere()Adds an additional WHERE condition to the existing one.
andWith()Causes the query to return matching {elements} eager-loaded with related elements, in addition to the elements that were already specified by with().
anyStatus()Clears out the status() and enabledForSite() parameters.
archived()Sets the \craft\elements\db\$archived property.
asArray()Causes the query to return matching {elements} as arrays of data, rather than [[{element-class}]] objects.
count()Returns the number of records.
dateCreated()Narrows the query results based on the {elements}’ creation dates.
dateUpdated()Narrows the query results based on the {elements}’ last-updated dates.
descendantDist()Narrows the query results to only {elements} that are up to a certain distance away from the {element} specified by descendantOf().
descendantOf()Narrows the query results to only {elements} that are descendants of another {element}.
emulateExecution()Sets whether to emulate query execution, preventing any interaction with data storage.
enabledForSite()Narrows the query results based on whether the {elements} are enabled in the site they’re being queried in, per the site() parameter.
exists()Returns a value indicating whether the query result contains any row of data.
extraFields()Returns the list of additional fields that can be returned by toArray() in addition to those listed in fields().
fields()Returns the list of fields that should be returned by default by toArray() when no specific fields are specified.
filterWhere()Sets the WHERE part of the query ignoring empty parameters.
fixedOrder()Causes the query results to be returned in the order specified by id().
hasDescendants()Narrows the query results based on whether the {elements} have any descendants.
id()Narrows the query results based on the {elements}’ IDs.
ids()Executes the query and returns the IDs of the resulting elements.
inReverse()Causes the query results to be returned in reverse order.
indexBy()Sets the indexBy() property.
leaves()Narrows the query results based on whether the {elements} are “leaves” ({elements} with no descendants).
level()Narrows the query results based on the {elements}’ level within the structure.
limit()Sets the LIMIT part of the query.
nextSiblingOf()Narrows the query results to only the {element} that comes immediately after another {element}.
nth()Executes the query and returns a single row of result at a given offset.
offset()Sets the OFFSET part of the query.
one()Executes the query and returns a single row of result.
orFilterWhere()Adds an additional WHERE condition to the existing one ignoring empty parameters.
orWhere()Adds an additional WHERE condition to the existing one.
orderBy()Sets the ORDER BY part of the query.
positionedAfter()Narrows the query results to only {elements} that are positioned after another {element}.
positionedBefore()Narrows the query results to only {elements} that are positioned before another {element}.
prevSiblingOf()Narrows the query results to only the {element} that comes immediately before another {element}.
ref()Narrows the query results based on a reference string.
relatedTo()Narrows the query results to only {elements} that are related to certain other elements.
search()Narrows the query results to only {elements} that match a search query.
siblingOf()Narrows the query results to only {elements} that are siblings of another {element}.
site()Determines which site the {elements} should be queried in.
siteId()Determines which site the {elements} should be queried in, per the site’s ID.
slug()Narrows the query results based on the {elements}’ slugs.
status()Narrows the query results based on the {elements}’ statuses.
structureId()Determines which structure data should be joined into the query.
title()Narrows the query results based on the {elements}’ titles.
toArray()Converts the object into an array.
uid()Narrows the query results based on the {elements}’ UIDs.
uri()Narrows the query results based on the {elements}’ URIs.
where()Sets the WHERE part of the query.
with()Causes the query to return matching {elements} eager-loaded with related elements.
withStructure()Explicitly determines whether the query should join in the structure data.

all()

Executes the query and returns all results as an array.

View source

Arguments

  • $db (yii\db\Connection, null) – The database connection used to generate the SQL statement. If this parameter is not given, the db application component will be used.

Returns

craft\base\ElementInterface[] – The resulting elements.

Signature

public abstract craft\base\ElementInterface[] all ( $db = null )

ancestorDist()

Narrows the query results to only {elements} that are up to a certain distance away from the {element} specified by ancestorOf().

View source

Arguments

Returns

static – Self reference

Signature

public abstract static ancestorDist ( \craft\elements\db\int $value = null )

Example

ancestorOf()

Narrows the query results to only {elements} that are ancestors of another {element}. Possible values include:

ValueFetches {elements}…
1above the {element} with an ID of 1.
a [[{element-class}]] objectabove the {element} represented by the object.

TIP

This can be combined with ancestorDist() if you want to limit how far away the ancestor {elements} can be.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static ancestorOf ( $value )

Example

andWith()

Causes the query to return matching {elements} eager-loaded with related elements, in addition to the elements that were already specified by with(). .

View source

Arguments

Returns

self – The query object itself

Signature

public abstract self andWith ( $value )

anyStatus()

Clears out the status() and enabledForSite() parameters.

View source

Returns

static – Self reference

Signature

public abstract static anyStatus ( )

Example

archived()

Sets the \craft\elements\db\$archived property.

View source

Arguments

  • $value (boolean) – The property value (defaults to true)

Returns

static – Self reference

Signature

public abstract static archived ( \craft\elements\db\bool $value = true )

asArray()

Causes the query to return matching {elements} as arrays of data, rather than [[{element-class}]] objects.

View source

Arguments

  • $value (boolean) – The property value (defaults to true)

Returns

static – Self reference

Signature

public abstract static asArray ( \craft\elements\db\bool $value = true )

Example

dateCreated()

Narrows the query results based on the {elements}’ creation dates. Possible values include:

ValueFetches {elements}…
'>= 2018-04-01'that were created on or after 2018-04-01.
'< 2018-05-01'that were created before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']that were created between 2018-04-01 and 2018-05-01.

View source

Arguments

  • $value (mixed) – The property value

Returns

static – Self reference

Signature

public abstract static dateCreated ( $value )

Example

dateUpdated()

Narrows the query results based on the {elements}’ last-updated dates. Possible values include:

ValueFetches {elements}…
'>= 2018-04-01'that were updated on or after 2018-04-01.
'< 2018-05-01'that were updated before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']that were updated between 2018-04-01 and 2018-05-01.

View source

Arguments

  • $value (mixed) – The property value

Returns

static – Self reference

Signature

public abstract static dateUpdated ( $value )

Example

descendantDist()

Narrows the query results to only {elements} that are up to a certain distance away from the {element} specified by descendantOf().

View source

Arguments

Returns

static – Self reference

Signature

public abstract static descendantDist ( \craft\elements\db\int $value = null )

Example

descendantOf()

Narrows the query results to only {elements} that are descendants of another {element}. Possible values include:

ValueFetches {elements}…
1below the {element} with an ID of 1.
a [[{element-class}]] objectbelow the {element} represented by the object.

TIP

This can be combined with descendantDist() if you want to limit how far away the descendant {elements} can be.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static descendantOf ( $value )

Example

enabledForSite()

Narrows the query results based on whether the {elements} are enabled in the site they’re being queried in, per the site() parameter. Possible values include:

ValueFetches {elements}…
true(default)that are enabled in the site.
falsewhether they are enabled or not in the site.

View source

Arguments

  • $value (boolean) – The property value (defaults to true)

Returns

static – Self reference

Signature

public abstract static enabledForSite ( \craft\elements\db\bool $value = true )

Example

fixedOrder()

Causes the query results to be returned in the order specified by id().

View source

Arguments

  • $value (boolean) – The property value (defaults to true)

Returns

static – Self reference

Signature

public abstract static fixedOrder ( \craft\elements\db\bool $value = true )

Example

hasDescendants()

Narrows the query results based on whether the {elements} have any descendants. (This has the opposite effect of calling leaves().)

View source

Arguments

  • $value (boolean) – The property value

Returns

static – Self reference

Signature

public abstract static hasDescendants ( \craft\elements\db\bool $value = true )

Example

id()

Narrows the query results based on the {elements}’ IDs. Possible values include:

ValueFetches {elements}…
1with an ID of 1.
'not 1'not with an ID of 1.
[1, 2]with an ID of 1 or 2.
['not', 1, 2]not with an ID of 1 or 2.

TIP

This can be combined with fixedOrder() if you want the results to be returned in a specific order.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static id ( $value )

Example

ids()

Executes the query and returns the IDs of the resulting elements.

View source

Arguments

  • $db (yii\db\Connection, null) – The database connection used to generate the SQL statement. If this parameter is not given, the db application component will be used.

Returns

integer[] – The resulting element IDs. An empty array is returned if no elements are found.

Signature

public abstract integer[] ids ( $db = null )

inReverse()

Causes the query results to be returned in reverse order.

View source

Arguments

  • $value (boolean) – The property value

Returns

static – Self reference

Signature

public abstract static inReverse ( \craft\elements\db\bool $value = true )

Example

leaves()

Narrows the query results based on whether the {elements} are “leaves” ({elements} with no descendants). (This has the opposite effect of calling hasDescendants().)

View source

Arguments

  • $value (boolean) – The property value

Returns

static – Self reference

Signature

public abstract static leaves ( \craft\elements\db\bool $value = true )

Example

level()

Narrows the query results based on the {elements}’ level within the structure. Possible values include:

ValueFetches {elements}…
1with a level of 1.
'not 1'not with a level of 1.
'>= 3'with a level greater than or equal to 3.
[1, 2]with a level of 1 or 2
['not', 1, 2]not with level of 1 or 2.

View source

Arguments

  • $value (mixed) – The property value

Returns

static – Self reference

Signature

public abstract static level ( $value = null )

Example

nextSiblingOf()

Narrows the query results to only the {element} that comes immediately after another {element}. Possible values include:

ValueFetches the {element}…
1after the {element} with an ID of 1.
a [[{element-class}]] objectafter the {element} represented by the object.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static nextSiblingOf ( $value )

Example

nth()

Executes the query and returns a single row of result at a given offset.

View source

Arguments

  • $n (integer) – The offset of the row to return. If offset() is set, $offset will be added to it.
  • $db (yii\db\Connection, null) – The database connection used to generate the SQL statement. If this parameter is not given, the db application component will be used.

Returns

craft\base\ElementInterface, array, null – The element or row of the query result. Null is returned if the query results in nothing.

Signature

public abstract craft\base\ElementInterface, array, null nth ( \craft\elements\db\int $n, yii\db\Connection $db = null )

one()

Executes the query and returns a single row of result.

View source

Arguments

  • $db (yii\db\Connection) – The database connection used to execute the query. If this parameter is not given, the db application component will be used.

Returns

craft\base\ElementInterface, array, null – The resulting element. Null is returned if the query results in nothing.

Signature

public abstract craft\base\ElementInterface, array, null one ( $db = null )

positionedAfter()

Narrows the query results to only {elements} that are positioned after another {element}. Possible values include:

ValueFetches {elements}…
1after the {element} with an ID of 1.
a [[{element-class}]] objectafter the {element} represented by the object.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static positionedAfter ( $value )

Example

positionedBefore()

Narrows the query results to only {elements} that are positioned before another {element}. Possible values include:

ValueFetches {elements}…
1before the {element} with an ID of 1.
a [[{element-class}]] objectbefore the {element} represented by the object.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static positionedBefore ( $value )

Example

prevSiblingOf()

Narrows the query results to only the {element} that comes immediately before another {element}. Possible values include:

ValueFetches the {element}…
1before the {element} with an ID of 1.
a [[{element-class}]] objectbefore the {element} represented by the object.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static prevSiblingOf ( $value )

Example

ref()

Narrows the query results based on a reference string.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static ref ( $value )

relatedTo()

Narrows the query results to only {elements} that are related to certain other elements. See Relations for a full explanation of how to work with this parameter.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static relatedTo ( $value )

Example

Narrows the query results to only {elements} that match a search query. See Searching for a full explanation of how to work with this parameter.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static search ( $value )

Example

siblingOf()

Narrows the query results to only {elements} that are siblings of another {element}. Possible values include:

ValueFetches {elements}…
1beside the {element} with an ID of 1.
a [[{element-class}]] objectbeside the {element} represented by the object.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static siblingOf ( $value )

Example

site()

Determines which site the {elements} should be queried in. The current site will be used by default.

Possible values include:

ValueFetches {elements}…
'foo'from the site with a handle of foo.
a craft\models\Site objectfrom the site represented by the object.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static site ( $value )

Example

siteId()

Determines which site the {elements} should be queried in, per the site’s ID. The current site will be used by default.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static siteId ( \craft\elements\db\int $value = null )

Example

slug()

Narrows the query results based on the {elements}’ slugs. Possible values include:

ValueFetches {elements}…
'foo'with a slug of foo.
'foo*'with a slug that begins with foo.
'*foo'with a slug that ends with foo.
'*foo*'with a slug that contains foo.
'not *foo*'with a slug that doesn’t contain foo.
['*foo*', '*bar*'with a slug that contains foo or bar.
['not', '*foo*', '*bar*']with a slug that doesn’t contain foo or bar.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static slug ( $value )

Example

status()

Narrows the query results based on the {elements}’ statuses. Possible values include:

ValueFetches {elements}…
'enabled'(default)that are enabled.
'disabled'that are disabled.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static status ( $value )

Example

structureId()

Determines which structure data should be joined into the query.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static structureId ( \craft\elements\db\int $value = null )

title()

Narrows the query results based on the {elements}’ titles. Possible values include:

ValueFetches {elements}…
'Foo'with a title of Foo.
'Foo*'with a title that begins with Foo.
'*Foo'with a title that ends with Foo.
'*Foo*'with a title that contains Foo.
'not *Foo*'with a title that doesn’t contain Foo.
['*Foo*', '*Bar*'with a title that contains Foo or Bar.
['not', '*Foo*', '*Bar*']with a title that doesn’t contain Foo or Bar.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static title ( $value )

Example

uid()

Narrows the query results based on the {elements}’ UIDs.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static uid ( $value )

Example

uri()

Narrows the query results based on the {elements}’ URIs. Possible values include:

ValueFetches {elements}…
'foo'with a URI of foo.
'foo*'with a URI that begins with foo.
'*foo'with a URI that ends with foo.
'*foo*'with a URI that contains foo.
'not *foo*'with a URI that doesn’t contain foo.
['*foo*', '*bar*'with a URI that contains foo or bar.
['not', '*foo*', '*bar*']with a URI that doesn’t contain foo or bar.

View source

Arguments

Returns

static – Self reference

Signature

public abstract static uri ( $value )

Example

with()

Causes the query to return matching {elements} eager-loaded with related elements. See Eager-Loading Elements for a full explanation of how to work with this parameter.

View source

Arguments

Returns

self – The query object itself

Signature

public abstract self with ( $value )

Example

withStructure()

Explicitly determines whether the query should join in the structure data.

View source

Arguments

  • $value (boolean) – The property value (defaults to true)

Returns

static – Self reference

Signature

public abstract static withStructure ( \craft\elements\db\bool $value = true )