It's used to set the query options for the query.
| public abstract static primaryModel ( ?\ | ||
| $value | ?\ |
|
public function primaryModel(?ActiveRecordInterface $value): static;
| Extends | Yiisoft\ |
|---|---|
| Implemented by | Yiisoft\ |
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.
Define an alias for the table defined in {@see ActiveRecordInterface}.
This method will adjust {@see \
If none was defined, {@see \
| public abstract static alias ( string $alias ) | ||
| $alias | string |
The table alias. |
| throws | \ |
|
|---|---|---|
| throws | \ |
|
| throws | \ |
|
public function alias(string $alias): static;
| public abstract Yiisoft\ | ||
| return | Yiisoft\ |
All rows of the query result. Each array element is an |
|---|---|---|
| throws | \ |
|
| throws | \ |
|
| throws | Throwable | |
public function all(): array;
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 static andOn ( array|\ | ||
| $condition | array|\ |
The new |
| $params | array |
The parameters (name => value) to be bound to the query. |
public function andOn(array|ExpressionInterface|string $condition, array $params = []): static;
Sets the {@see ActiveQuery::$asArray} property.
| public abstract static asArray ( boolean|null $value = true ) | ||
| $value | boolean|null |
Whether to return the query results in terms of arrays instead of Active Records. |
| return | static |
The query object itself. |
|---|---|---|
public function asArray(?bool $value = true): static;
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|\ | ||
| $values | array|float|integer|string | |
public function findByPk(array|float|int|string $values): array|ActiveRecordInterface|null;
| public abstract string|null getInverseOf ( ) | ||
| return | string|null |
The name of the relation that is the inverse of this relation. |
|---|---|---|
public function getInverseOf(): ?string;
| public abstract array getJoinsWith ( ) | ||
| return | array |
A list of relations that this query should be joined with. |
|---|---|---|
public function getJoinsWith(): array;
| 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. |
|---|---|---|
public function getLink(): array;
| public abstract Yiisoft\ | ||
| return | Yiisoft\ |
The model instance associated with this query. |
|---|---|---|
public function getModel(): ActiveRecordInterface;
| public abstract array|\ | ||
| return | array|\ |
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 \ Please refer to {@see \ |
|---|---|---|
public function getOn(): array|ExpressionInterface|string|null;
| public abstract Yiisoft\ | ||
| return | Yiisoft\ |
The primary model of a relational query. This is used only in lazy loading with dynamic query options. |
|---|---|---|
public function getPrimaryModel(): ?ActiveRecordInterface;
| public abstract string|null getSql ( ) | ||
| return | string|null |
The SQL statement to be executed for retrieving AR records. This is set by {@see \ |
|---|---|---|
public function getSql(): ?string;
Returns table names used in {@see QueryPartsInterface::from()} indexed by aliases.
Both aliases and names are enclosed into {{ and }}.
| public abstract array getTablesUsedInFrom ( ) | ||
| throws | \ |
|
|---|---|---|
| throws | InvalidArgumentException | |
| throws | \ |
|
| throws | \ |
|
public function getTablesUsedInFrom(): array;
| public abstract Yiisoft\ | ||
| return | Yiisoft\ |
The query associated with the junction table.
Please call {@see \ This property is only used in relational context. |
|---|---|---|
public function getVia(): array|self|null;
| public abstract array getWith ( ) | ||
| return | array |
A list of relations that this query should be performed with. |
|---|---|---|
public function getWith(): array;
Inner joins with the specified relations.
This is a shortcut method to {@see \
Please refer to {@see \
See also Yiisoft\
| public abstract static 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;
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 \
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 static inverseOf ( string $relationName ) | ||
| $relationName | string |
The name of the relation that is the inverse of this relation. |
| return | static |
The relation object itself. |
|---|---|---|
public function inverseOf(string $relationName): static;
Returns a value indicating whether the query result rows should be returned as arrays instead of Active Record models.
| public abstract ?bool isAsArray ( ) |
public function isAsArray(): ?bool;
| 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 If |
|---|---|---|
public function isMultiple(): bool;
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 \
Note: That because a JOIN query will be performed, you're responsible for disambiguated column names.
This method differs from {@see \JOIN SQL
statement for the primary table. And when $eagerLoading is true, it will
call {@see \
Note: Relations specified in $with cannot have GROUP BY, HAVING, or UNION clauses. Using these clauses
will result in a {@see \
| public abstract static 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:
The relation name may optionally contain an alias for the relation table (for example, Sub-relations can also be specified, see {@see \ In the following, you find some examples:
|
| $eagerLoading | array|boolean |
Whether to eager load the relations specified in |
| $joinType | array|string |
The join type of the relations specified in |
public function joinWith(
array|string $with,
array|bool $eagerLoading = true,
array|string $joinType = 'LEFT JOIN',
): static;
It's used to set the query options for the query.
| public abstract static 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. |
public function link(array $value): static;
It's used to set the query options for the query.
| public abstract static 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 \ |
public function multiple(bool $value): static;
Sets the ON condition for a relational query.
The condition will be used in the ON part when {@see \
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 \
public function getActiveUsers(): ActiveQuery
{
return $this->hasMany(User::class, ['id' => 'user_id'])->on(['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 static on ( array|\ | ||
| $condition | array|\ |
The ON condition. Please refer to {@see \ |
| $params | array |
The parameters (name => value) to be bound to the query. |
public function on(array|ExpressionInterface|string $condition, array $params = []): static;
| public abstract Yiisoft\ | ||
| return | Yiisoft\ |
The first row as an |
|---|---|---|
| throws | \ |
|
| throws | InvalidArgumentException | |
| throws | \ |
|
| throws | \ |
|
| throws | ReflectionException | |
| throws | Throwable | |
public function one(): array|ActiveRecordInterface|null;
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 static orOn ( array|\ | ||
| $condition | array|\ |
The new |
| $params | array |
The parameters (name => value) to be bound to the query. |
public function orOn(array|ExpressionInterface|string $condition, array $params = []): static;
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\ | ||
| $rows | array[] |
The raw query result from a database. |
| return | Yiisoft\ |
The converted query result. |
|---|---|---|
public function populate(array $rows): array;
Finds the related records and populates them into the primary models.
| public abstract Yiisoft\ | ||
| $name | string |
The relation name. |
| $primaryModels | Yiisoft\ |
Primary models. |
| return | Yiisoft\ |
The related models. |
|---|---|---|
| throws | \ |
|
| throws | \ |
If
{@see \ |
public function populateRelation(string $name, array &$primaryModels): array;
It's used to set the query options for the query.
| public abstract static primaryModel ( ?\ | ||
| $value | ?\ |
|
public function primaryModel(?ActiveRecordInterface $value): static;
Resets the relations that this query should be performed with.
This method clears all relations set via {@see \
| public abstract static resetWith ( ) | ||
| return | static |
The query object itself. |
|---|---|---|
public function resetWith(): static;
| public abstract static sql ( ?string $value ) | ||
| $value | ?string | |
public function sql(?string $value): static;
Specifies the relation associated with the junction table.
Use this method to specify a pivot record/table when declaring a relation in the {@see \
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 static via ( string $relationName, callable|null $callable = null ) | ||
| $relationName | string |
The relation name. This refers to a relation declared
in {@see \ |
| $callable | callable|null |
A PHP callback for customizing the relation associated with the junction table.
Its signature should be |
| return | static |
The relation object itself. |
|---|---|---|
public function via(string $relationName, ?callable $callable = null): static;
Specifies the junction table for a relational query.
Use this method to specify a junction table when declaring a relation in the {@see \
public function getItems()
{
return $this->hasMany(Item::class, ['id' => 'item_id'])->viaTable('order_item', ['order_id' => 'id']);
}
| public abstract static 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 \ |
| $callable | callable|null |
A PHP callback for customizing the relation associated with the junction table.
Its signature should be |
public function viaTable(string $tableName, array $link, ?callable $callable = null): static;
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 \
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 static with ( array|string $with ) | ||
| $with | array|string |
A list of relation names or relation definitions. |
| return | static |
The query object itself. |
|---|---|---|
public function with(array|string ...$with): static;
User Contributed Notes
Leave a comment
Join the conversation to share a note.