Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
360d5a4e
Commit
360d5a4e
authored
Jan 20, 2015
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #6166 from cdvrooman/patch-23
[skip ci] Update db-query-builder.md
parents
cd1e9f4a
732462a6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
10 deletions
+10
-10
db-query-builder.md
docs/guide/db-query-builder.md
+10
-10
No files found.
docs/guide/db-query-builder.md
View file @
360d5a4e
...
...
@@ -34,7 +34,7 @@ $rows = $command->queryAll();
Query Methods
-------------
As you can see,
[
[yii\db\Query
]
] is the main player that you need to deal with. Behind the scene,
As you can see,
[
[yii\db\Query
]
] is the main player that you need to deal with. Behind the scene
s
,
`Query`
is actually only responsible for representing various query information. The actual query
building logic is done by
[
[yii\db\QueryBuilder
]
] when you call the
`createCommand()`
method,
and the query execution is done by
[
[yii\db\Command
]
].
...
...
@@ -48,7 +48,7 @@ the query, execute it, and return the result. For example,
-
[
[yii\db\Query::scalar()|scalar()
]
]: returns the first column in the first row of the result.
-
[
[yii\db\Query::exists()|exists()
]
]: returns a value indicating whether the query results in anything.
-
[
[yii\db\Query::count()|count()
]
]: returns the result of a
`COUNT`
query. Other similar methods
include
`sum($q)`
,
`average($q)`
,
`max($q)`
,
`min($q)`
, which support the so-called aggregational data query.
`$q`
include
`sum($q)`
,
`average($q)`
,
`max($q)`
and
`min($q)`
, which support the so-called aggregational data query. The
`$q`
parameter is mandatory for these methods and can be either the column name or expression.
...
...
@@ -82,7 +82,7 @@ $query->select(['id', 'name'])
> by commas, which is not what you want to see.
When specifying columns, you may include the table prefixes or column aliases, e.g.,
`user.id`
,
`user.id AS user_id`
.
If you are using array to specify the columns, you may also use the array keys to specify the column aliases,
If you are using a
n a
rray to specify the columns, you may also use the array keys to specify the column aliases,
e.g.,
`['user_id' => 'user.id', 'user_name' => 'user.name']`
.
Starting from version 2.0.1, you may also select sub-queries as columns. For example,
...
...
@@ -118,7 +118,7 @@ $query->select('u.*, p.*')->from(['user u', 'post p']);
```
When the tables are specified as an array, you may also use the array keys as the table aliases
(if a table does not need alias, do not use a string key). For example,
(if a table does not need a
n a
lias, do not use a string key). For example,
```
php
$query
->
select
(
'u.*, p.*'
)
->
from
([
'u'
=>
'user'
,
'p'
=>
'post'
]);
...
...
@@ -408,7 +408,7 @@ $query->leftJoin(['u' => $subQuery], 'u.id=author_id');
### `UNION`
`UNION`
in SQL adds results of one query to results of another query. Columns returned by both queries should match.
In Yii in order to build it you can first form two query objects and then use
`union`
method:
In Yii in order to build it you can first form two query objects and then use
the
`union`
method:
```
php
$query
=
new
Query
();
...
...
@@ -424,9 +424,9 @@ $query->union($anotherQuery);
Batch Query
-----------
When working with large amount of data, methods such as
[
[yii\db\Query::all()
]
] are not suitable
When working with large amount
s
of data, methods such as
[
[yii\db\Query::all()
]
] are not suitable
because they require loading all data into the memory. To keep the memory requirement low, Yii
provides the so-called batch query support. A batch query makes uses of data cursor and fetches
provides the so-called batch query support. A batch query makes uses of
the
data cursor and fetches
data in batches.
Batch query can be used like the following:
...
...
@@ -450,12 +450,12 @@ foreach ($query->each() as $user) {
The method
[
[yii\db\Query::batch()
]
] and
[
[yii\db\Query::each()
]
] return an
[
[yii\db\BatchQueryResult
]
] object
which implements the
`Iterator`
interface and thus can be used in the
`foreach`
construct.
During the first iteration, a SQL query is made to the database. Data are
since
then fetched in batches
in the iterations. By default, the batch size is 100, meaning 100 rows of data are being fetched in each batch.
During the first iteration, a SQL query is made to the database. Data are then fetched in batches
in the
remaining
iterations. By default, the batch size is 100, meaning 100 rows of data are being fetched in each batch.
You can change the batch size by passing the first parameter to the
`batch()`
or
`each()`
method.
Compared to the
[
[yii\db\Query::all()
]
], the batch query only loads 100 rows of data at a time into the memory.
If you process the data and then discard it right away, the batch query can help
keep the memory usage under a limit
.
If you process the data and then discard it right away, the batch query can help
reduce memory usage
.
If you specify the query result to be indexed by some column via
[
[yii\db\Query::indexBy()
]
], the batch query
will still keep the proper index. For example,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment