0 follower

Interface Yiisoft\ActiveRecord\ActiveQueryInterface

ExtendsYiisoft\Db\Query\QueryInterface
Implemented byYiisoft\ActiveRecord\ActiveQuery

A common interface to be implemented by active record query classes.

That are methods for all normal queries that return active records but also relational queries in which the query represents a relation between two active record classes and will return related records only.

Public Methods

Hide inherited methods

Method Description Defined By
alias() Define an alias for the table defined in {@see ActiveRecordInterface}. Yiisoft\ActiveRecord\ActiveQueryInterface
all() Yiisoft\ActiveRecord\ActiveQueryInterface
andOn() Adds ON condition to the existing one. Yiisoft\ActiveRecord\ActiveQueryInterface
asArray() Sets the {@see asArray} property. Yiisoft\ActiveRecord\ActiveQueryInterface
findByPk() Finds an ActiveRecord instance by the given primary key value. Yiisoft\ActiveRecord\ActiveQueryInterface
getInverseOf() Yiisoft\ActiveRecord\ActiveQueryInterface
getJoinsWith() Yiisoft\ActiveRecord\ActiveQueryInterface
getLink() Yiisoft\ActiveRecord\ActiveQueryInterface
getModel() Yiisoft\ActiveRecord\ActiveQueryInterface
getOn() Yiisoft\ActiveRecord\ActiveQueryInterface
getPrimaryModel() Yiisoft\ActiveRecord\ActiveQueryInterface
getSql() Yiisoft\ActiveRecord\ActiveQueryInterface
getTablesUsedInFrom() Returns table names used in {@see from} indexed by aliases. Yiisoft\ActiveRecord\ActiveQueryInterface
getVia() Yiisoft\ActiveRecord\ActiveQueryInterface
getWith() Yiisoft\ActiveRecord\ActiveQueryInterface
innerJoinWith() Inner joins with the specified relations. Yiisoft\ActiveRecord\ActiveQueryInterface
inverseOf() Sets the name of the relation that is the inverse of this relation. Yiisoft\ActiveRecord\ActiveQueryInterface
isAsArray() Returns a value indicating whether the query result rows should be returned as arrays instead of Active Record models. Yiisoft\ActiveRecord\ActiveQueryInterface
isMultiple() Yiisoft\ActiveRecord\ActiveQueryInterface
joinWith() Joins with the specified relations. Yiisoft\ActiveRecord\ActiveQueryInterface
link() It's used to set the query options for the query. Yiisoft\ActiveRecord\ActiveQueryInterface
multiple() It's used to set the query options for the query. Yiisoft\ActiveRecord\ActiveQueryInterface
on() Sets the ON condition for a relational query. Yiisoft\ActiveRecord\ActiveQueryInterface
one() Yiisoft\ActiveRecord\ActiveQueryInterface
orOn() Adds ON condition to the existing one. Yiisoft\ActiveRecord\ActiveQueryInterface
populate() Converts the raw query results into the format as specified by this query. Yiisoft\ActiveRecord\ActiveQueryInterface
populateRelation() Finds the related records and populates them into the primary models. Yiisoft\ActiveRecord\ActiveQueryInterface
primaryModel() It's used to set the query options for the query. Yiisoft\ActiveRecord\ActiveQueryInterface
relatedRecords() Returns related record(s). Yiisoft\ActiveRecord\ActiveQueryInterface
resetJoinsWith() Yiisoft\ActiveRecord\ActiveQueryInterface
resetVia() Yiisoft\ActiveRecord\ActiveQueryInterface
resetWith() Resets the relations that this query should be performed with. Yiisoft\ActiveRecord\ActiveQueryInterface
sql() Yiisoft\ActiveRecord\ActiveQueryInterface
via() Specifies the relation associated with the junction table. Yiisoft\ActiveRecord\ActiveQueryInterface
viaTable() Specifies the junction table for a relational query. Yiisoft\ActiveRecord\ActiveQueryInterface
with() Specifies the relations with which this query should be performed. Yiisoft\ActiveRecord\ActiveQueryInterface

Method Details

Hide inherited methods

alias() public abstract method

Define an alias for the table defined in {@see ActiveRecordInterface}.

This method will adjust {@see \Yiisoft\ActiveRecord\from()} so that an already defined alias will be overwritten.

If none was defined, {@see \Yiisoft\ActiveRecord\from()} will be populated with the given alias.

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface alias ( string $alias )
$alias string

The table alias.

throws \Yiisoft\Definitions\Exception\CircularReferenceException
throws \Yiisoft\Definitions\Exception\NotInstantiableException
throws \Yiisoft\Definitions\Exception\InvalidConfigException

                public function alias(string $alias): static;

            
all() public abstract method

public abstract Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] all ( )
return Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[]

All rows of the query result. Each array element is an array or instance of {@see \Yiisoft\ActiveRecord\ActiveRecordInterface} representing a row of data, depends on {@see \Yiisoft\ActiveRecord\isAsArray()} result. Empty array if the query results in nothing.

throws \Yiisoft\Db\Exception\Exception
throws \Yiisoft\Db\Exception\InvalidConfigException
throws Throwable

                public function all(): array;

            
andOn() public abstract method

Adds ON condition to the existing one.

The new condition and the existing one will be joined using the AND operator.

See also:

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface andOn ( array|\Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] )
$condition array|\Yiisoft\Db\Expression\ExpressionInterface|string

The new ON condition. Please refer to {@see \Yiisoft\ActiveRecord\where()} on how to specify this parameter.

$params array

The parameters (name => value) to be bound to the query.

                public function andOn(array|ExpressionInterface|string $condition, array $params = []): static;

            
asArray() public abstract method

Sets the {@see asArray} property.

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface asArray ( boolean|null $value true )
$value boolean|null

Whether to return the query results in terms of arrays instead of Active Records.

return Yiisoft\ActiveRecord\ActiveQueryInterface

The query object itself.

                public function asArray(?bool $value = true): static;

            
findByPk() public abstract method

Finds an ActiveRecord instance by the given primary key value.

In the examples below, the id column is the primary key of the table.

$customerQuery = Customer::query();

$customer = $customerQuery->findByPk(1); // WHERE id = 1
$customer = $customerQuery->findByPk([1]); // WHERE id = 1

In the examples below, the id and id2 columns are the composite primary key of the table.

$orderItemQuery = OrderItem::query();

$orderItem = $orderItemQuery->findByPk([1, 2]); // WHERE id = 1 AND id2 = 2

If you need to pass user input to this method, make sure the input value is scalar or in case of array, make sure the array values are scalar:

public function actionView(ServerRequestInterface $request)
{
    $id = (string) $request->getAttribute('id');

    $customerQuery = Customer::query();
    $customer = $customerQuery->findByPk($id);
}
public abstract array|Yiisoft\ActiveRecord\ActiveRecordInterface|null findByPk ( array|float|integer|string $values )
$values array|float|integer|string

                public function findByPk(array|float|int|string $values): array|ActiveRecordInterface|null;

            
getInverseOf() public abstract method

public abstract string|null getInverseOf ( )
return string|null

The name of the relation that is the inverse of this relation.

                public function getInverseOf(): ?string;

            
getJoinsWith() public abstract method

public abstract array getJoinsWith ( )
return array

A list of relations that this query should be joined with.

                public function getJoinsWith(): array;

            
getLink() public abstract method

public abstract string[] getLink ( )
return string[]

The columns of the primary and foreign tables that establish a relation.

The array keys must be columns of the table for this relation, and the array values must be the corresponding columns from the primary table.

Don't prefix or quote the column names. Yii does that automatically. This property is only used in relational context.

getModel() public abstract method

public abstract Yiisoft\ActiveRecord\ActiveRecordInterface getModel ( )
return Yiisoft\ActiveRecord\ActiveRecordInterface

The model instance associated with this query.

                public function getModel(): ActiveRecordInterface;

            
getOn() public abstract method

See also on().

public abstract array|\Yiisoft\Db\Expression\ExpressionInterface|string|null getOn ( )
return array|\Yiisoft\Db\Expression\ExpressionInterface|string|null

The join condition to be used when this query is used in a relational context.

The condition will be used in the ON part when {@see \Yiisoft\ActiveRecord\joinWith()} is called. Otherwise, the condition will be used in the WHERE part of a query.

Please refer to {@see \Yiisoft\ActiveRecord\Query::where()} on how to specify this parameter.

                public function getOn(): array|ExpressionInterface|string|null;

            
getPrimaryModel() public abstract method

public abstract Yiisoft\ActiveRecord\ActiveRecordInterface|null getPrimaryModel ( )
return Yiisoft\ActiveRecord\ActiveRecordInterface|null

The primary model of a relational query.

This is used only in lazy loading with dynamic query options.

                public function getPrimaryModel(): ?ActiveRecordInterface;

            
getSql() public abstract method

public abstract string|null getSql ( )
return string|null

The SQL statement to be executed for retrieving AR records.

This is set by {@see \Yiisoft\ActiveRecord\ActiveRecord::findBySql()}.

                public function getSql(): ?string;

            
getTablesUsedInFrom() public abstract method

Returns table names used in {@see from} indexed by aliases.

Both aliases and names are enclosed into {{ and }}.

public abstract array getTablesUsedInFrom ( )
throws \Yiisoft\Definitions\Exception\CircularReferenceException
throws InvalidArgumentException
throws \Yiisoft\Definitions\Exception\NotInstantiableException
throws \Yiisoft\Definitions\Exception\InvalidConfigException

                public function getTablesUsedInFrom(): array;

            
getVia() public abstract method

See also via().

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface|array|null getVia ( )
return Yiisoft\ActiveRecord\ActiveQueryInterface|array|null

The query associated with the junction table. Please call {@see \Yiisoft\ActiveRecord\via()} to set this property instead of directly setting it.

This property is only used in relational context.

                public function getVia(): array|self|null;

            
getWith() public abstract method

public abstract array getWith ( )
return array

A list of relations that this query should be performed with.

                public function getWith(): array;

            
innerJoinWith() public abstract method

Inner joins with the specified relations.

This is a shortcut method to {@see \Yiisoft\ActiveRecord\joinWith()} with the join type set as "INNER JOIN".

Please refer to {@see \Yiisoft\ActiveRecord\joinWith()} for detailed usage of this method.

See also joinWith().

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface innerJoinWith ( array|string $with, array|boolean $eagerLoading true )
$with array|string

The relations to be joined with.

$eagerLoading array|boolean

Whether to eager load the relations. Note: That this doesn't mean that the relations are populated from the query result. An extra query will still be performed to bring in the related data.

                public function innerJoinWith(array|string $with, array|bool $eagerLoading = true): static;

            
inverseOf() public abstract method

Sets the name of the relation that is the inverse of this relation.

For example, a customer has orders, which means the inverse of the "orders" relation is the "customer".

If this property is set, the primary record(s) will be referenced through the specified relation.

For example, $customer->orders[0]->customer and $customer will be the same object, and accessing the customer of an order will not trigger a new DB query.

Use this method when declaring a relation in the {@see \Yiisoft\ActiveRecord\ActiveRecord} class, e.g., in the Customer model:

public function getOrdersQuery()
{
    return $this->hasMany(Order::class, ['customer_id' => 'id'])->inverseOf('customer');
}

This also may be used for the Order model, but with caution:

public function getCustomerQuery()
{
    return $this->hasOne(Customer::class, ['id' => 'customer_id'])->inverseOf('orders');
}

in this case result will depend on how order(s) was loaded. Let's suppose customer has several orders. If only one order was loaded:

$orders = Order::query()->where(['id' => 1])->all();
$customerOrders = $orders[0]->customer->orders;

variable $customerOrders will contain only one order. If orders was loaded like this:

$orders = Order::query()->with('customer')->where(['customer_id' => 1])->all();
$customerOrders = $orders[0]->customer->orders;

variable $customerOrders will contain all orders of the customer.

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface inverseOf ( string $relationName )
$relationName string

The name of the relation that is the inverse of this relation.

return Yiisoft\ActiveRecord\ActiveQueryInterface

The relation object itself.

                public function inverseOf(string $relationName): static;

            
isAsArray() public abstract method

Returns a value indicating whether the query result rows should be returned as arrays instead of Active Record models.

public abstract boolean|null isAsArray ( )

                public function isAsArray(): ?bool;

            
isMultiple() public abstract method

public abstract boolean isMultiple ( )
return boolean

Whether this query represents a relation to more than one record.

This property is only used in relational context.

If true, this relation will populate all query results into active record instances using {@see \Yiisoft\ActiveRecord\all()}.

If false, only the first row of the results will be retrieved using {@see \Yiisoft\ActiveRecord\one()}.

                public function isMultiple(): bool;

            
joinWith() public abstract method

Joins with the specified relations.

This method allows you to reuse existing relation definitions to perform JOIN queries. Based on the definition of the specified relation(s), the method will append one or many JOIN statements to the current query.

If the $eagerLoading parameter is true, the method will also perform eager loading for the specified relations, which is equal to calling {@see \Yiisoft\ActiveRecord\with()} using the specified relations.

Note: That because a JOIN query will be performed, you're responsible for disambiguated column names.

This method differs from {@see \Yiisoft\ActiveRecord\with()} in that it will build up and execute a JOIN SQL statement for the primary table. And when $eagerLoading is true, it will call {@see \Yiisoft\ActiveRecord\with()} also with the specified relations.

Note: Relations specified in $with cannot have GROUP BY, HAVING, or UNION clauses. Using these clauses will result in a {@see \LogicException}.

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface joinWith ( array|string $with, array|boolean $eagerLoading true, array|string $joinType 'LEFT JOIN' )
$with array|string

The relations to be joined. This can either be a string, representing a relation name or an array with the following semantics:

  • Each array element represents a single relation.
  • You may specify the relation name as the array key and give anonymous functions that can be used to change the relation queries on-the-fly as the array value.
  • If a relation query doesn't need modification, you may use the relation name as the array value.

The relation name may optionally contain an alias for the relation table (for example, books b).

Sub-relations can also be specified, see {@see \Yiisoft\ActiveRecord\with()} for the syntax.

In the following, you find some examples:

// Find all orders that contain books, and eager loading "books".
$orderQuery = Order::query();
$orderQuery->joinWith('books', true, 'INNER JOIN')->all();

// Find all orders, eagerly load "books", and sort the orders and books by the book names.
$orderQuery = Order::query();
$orderQuery->joinWith([
    'books' => function (ActiveQuery $query) {
        $query->orderBy('item.name');
    }
])->all();

// Find all orders that contain books of the category 'Science fiction', using the alias "b" for the book table.
$order = Order::query();
$orderQuery->joinWith(['books b'], true, 'INNER JOIN')->where(['b.category' => 'Science fiction'])->all();
$eagerLoading array|boolean

Whether to eager load the relations specified in $with. When this is boolean. It applies to all relations specified in $with. Use an array to explicitly list which relations in $with a need to be eagerly loaded. Note: This doesn't mean that the relations are populated from the query result. An extra query will still be performed to bring in the related data. Defaults to true.

$joinType array|string

The join type of the relations specified in $with. When this is a string, it applies to all relations specified in $with. Use an array in the format of relationName => joinType to specify different join types for different relations.

                public function joinWith(
    array|string $with,
    array|bool $eagerLoading = true,
    array|string $joinType = 'LEFT JOIN',
): static;

            
link() public abstract method

It's used to set the query options for the query.

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface link ( string[] $value )
$value string[]

The columns of the primary and foreign tables that establish a relation. The array keys must be columns of the table for this relation, and the array values must be the corresponding columns from the primary table. Don't prefix or quote the column names as Yii will do this automatically. This property is only used in relational context.

multiple() public abstract method

It's used to set the query options for the query.

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface multiple ( boolean $value )
$value boolean

Whether this query represents a relation to more than one record. This property is only used in relational context. If true, this relation will populate all query results into AR instances using {@see \Yiisoft\ActiveRecord\all()}. If false, only the first row of the results will be retrieved using {@see \Yiisoft\ActiveRecord\one()}.

                public function multiple(bool $value): static;

            
on() public abstract method

Sets the ON condition for a relational query.

The condition will be used in the ON part when {@see \Yiisoft\ActiveRecord\joinWith()} is called.

Otherwise, the condition will be used in the WHERE part of a query.

Use this method to specify more conditions when declaring a relation in the {@see \Yiisoft\ActiveRecord\ActiveRecord} class:

public function getActiveUsers(): ActiveQuery
{
    return $this->hasMany(User::class, ['id' => 'user_id'])->onCondition(['active' => true]);
}

Note that this condition is applied in case of a join as well as when fetching the related records. These only fields of the related table can be used in the condition. Trying to access fields of the primary record will cause an error in a non-join-query.

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface on ( array|\Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] )
$condition array|\Yiisoft\Db\Expression\ExpressionInterface|string

The ON condition. Please refer to {@see \Yiisoft\ActiveRecord\Query::where()} on how to specify this parameter.

$params array

The parameters (name => value) to be bound to the query.

                public function on(array|ExpressionInterface|string $condition, array $params = []): static;

            
one() public abstract method

public abstract Yiisoft\ActiveRecord\ActiveRecordInterface|array|null one ( )
return Yiisoft\ActiveRecord\ActiveRecordInterface|array|null

The first row as an array or instance of {@see \Yiisoft\ActiveRecord\ActiveRecordInterface} of the query result, depends on {@see \Yiisoft\ActiveRecord\isAsArray()} result. null if the query results in nothing.

throws \Yiisoft\Db\Exception\Exception
throws InvalidArgumentException
throws \Yiisoft\Db\Exception\InvalidConfigException
throws \Yiisoft\Db\Exception\NotSupportedException
throws ReflectionException
throws Throwable

                public function one(): array|ActiveRecordInterface|null;

            
orOn() public abstract method

Adds ON condition to the existing one.

The new condition and the existing one will be joined using the OR operator.

See also:

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface orOn ( array|\Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] )
$condition array|\Yiisoft\Db\Expression\ExpressionInterface|string

The new ON condition. Please refer to {@see \Yiisoft\ActiveRecord\where()} on how to specify this parameter.

$params array

The parameters (name => value) to be bound to the query.

                public function orOn(array|ExpressionInterface|string $condition, array $params = []): static;

            
populate() public abstract method

Converts the raw query results into the format as specified by this query.

This method is internally used to convert the data fetched from a database into the format as required by this query.

public abstract Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] populate ( array[] $rows )
$rows array[]

The raw query result from a database.

return Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[]

The converted query result.

                public function populate(array $rows): array;

            
populateRelation() public abstract method

Finds the related records and populates them into the primary models.

public abstract Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] populateRelation ( string $name, Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[] &$primaryModels )
$name string

The relation name.

$primaryModels Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[]

Primary models.

return Yiisoft\ActiveRecord\ActiveRecordInterface[]|array[]

The related models.

throws \Yiisoft\Db\Exception\Exception
throws InvalidArgumentException|\Yiisoft\Db\Exception\InvalidConfigException|\Yiisoft\Db\Exception\NotSupportedException|Throwable

If {@see \Yiisoft\ActiveRecord\link()} is invalid.

                public function populateRelation(string $name, array &$primaryModels): array;

            
primaryModel() public abstract method

It's used to set the query options for the query.

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface primaryModel ( Yiisoft\ActiveRecord\ActiveRecordInterface|null $value )
$value Yiisoft\ActiveRecord\ActiveRecordInterface|null

                public function primaryModel(?ActiveRecordInterface $value): static;

            
relatedRecords() public abstract method

Returns related record(s).

This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.

public abstract Yiisoft\ActiveRecord\ActiveRecordInterface|Yiisoft\ActiveRecord\ActiveRecordInterface[]|array|array[]|null relatedRecords ( )
return Yiisoft\ActiveRecord\ActiveRecordInterface|Yiisoft\ActiveRecord\ActiveRecordInterface[]|array|array[]|null

The related record(s).

throws \Yiisoft\Db\Exception\Exception
throws InvalidArgumentException
throws \Yiisoft\Db\Exception\InvalidConfigException
throws ReflectionException
throws Throwable

if the relation is invalid.

                public function relatedRecords(): ActiveRecordInterface|array|null;

            
resetJoinsWith() public abstract method

public abstract void resetJoinsWith ( )

                public function resetJoinsWith(): void;

            
resetVia() public abstract method

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface resetVia ( )

                public function resetVia(): static;

            
resetWith() public abstract method

Resets the relations that this query should be performed with.

This method clears all relations set via {@see \Yiisoft\ActiveRecord\with()} and disables eager loading for relations set via {@see \Yiisoft\ActiveRecord\joinWith()}, while keeping the JOIN clauses intact.

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface resetWith ( )
return Yiisoft\ActiveRecord\ActiveQueryInterface

The query object itself.

                public function resetWith(): static;

            
sql() public abstract method

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface sql ( string|null $value )
$value string|null

                public function sql(?string $value): static;

            
via() public abstract method

Specifies the relation associated with the junction table.

Use this method to specify a pivot record/table when declaring a relation in the {@see \Yiisoft\ActiveRecord\ActiveRecord} class:

class Order extends ActiveRecord
{
   public function getOrderItems() {
       return $this->hasMany(OrderItem::class, ['order_id' => 'id']);
   }

   public function getItems() {
       return $this->hasMany(Item::class, ['id' => 'item_id'])->via('orderItems');
   }
}
public abstract Yiisoft\ActiveRecord\ActiveQueryInterface via ( string $relationName, callable|null $callable null )
$relationName string

The relation name. This refers to a relation declared in {@see \Yiisoft\ActiveRecord\primaryModel}.

$callable callable|null

A PHP callback for customizing the relation associated with the junction table. Its signature should be function($query), where $query is the query to be customized.

return Yiisoft\ActiveRecord\ActiveQueryInterface

The relation object itself.

                public function via(string $relationName, ?callable $callable = null): static;

            
viaTable() public abstract method

Specifies the junction table for a relational query.

Use this method to specify a junction table when declaring a relation in the {@see \Yiisoft\ActiveRecord\ActiveRecord} class:

public function getItems()
{
    return $this->hasMany(Item::class, ['id' => 'item_id'])->viaTable('order_item', ['order_id' => 'id']);
}

See also via().

public abstract Yiisoft\ActiveRecord\ActiveQueryInterface viaTable ( string $tableName, string[] $link, callable|null $callable null )
$tableName string

The name of the junction table.

$link string[]

The link between the junction table and the table associated with {@see \Yiisoft\ActiveRecord\primaryModel}. The keys of the array represent the columns in the junction table, and the values represent the columns in the {@see \Yiisoft\ActiveRecord\primaryModel} table.

$callable callable|null

A PHP callback for customizing the relation associated with the junction table. Its signature should be function($query), where $query is the query to be customized.

                public function viaTable(string $tableName, array $link, ?callable $callable = null): static;

            
with() public abstract method

Specifies the relations with which this query should be performed.

The parameters to this method can be either one or multiple strings, or a single array of relation names and the optional callbacks to customize the relations.

A relation name can refer to a relation defined in {@see \Yiisoft\ActiveRecord\modelClass} or a sub-relation that stands for a relation of a related record.

For example, orders.address means the address relation defined in the model class corresponding to the orders relation.

The following are some usage examples:

// find customers together with their orders and country
Customer::query()->with('orders', 'country')->all();
// find customers together with their orders and the orders' shipping address
Customer::query()->with('orders.address')->all();
// find customers together with their country and orders of status 1
Customer::query()->with([
    'orders' => function (ActiveQuery $query) {
        $query->andWhere('status = 1');
    },
    'country',
])->all();

You can call with() multiple times. Each call will add relations to the existing ones.

For example, the following two statements are equivalent:

Customer::query()->with('orders', 'country')->all();
Customer::query()->with('orders')->with('country')->all();
public abstract Yiisoft\ActiveRecord\ActiveQueryInterface with ( array|string $with )
$with array|string

A list of relation names or relation definitions.

return Yiisoft\ActiveRecord\ActiveQueryInterface

The query object itself.

                public function with(array|string ...$with): static;