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
5829020e
Commit
5829020e
authored
Oct 19, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #5223: Query builder now supports selecting sub-queries as columns
parent
48ec791e
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
33 additions
and
1 deletion
+33
-1
db-query-builder.md
docs/guide/db-query-builder.md
+9
-0
CHANGELOG.md
extensions/sphinx/CHANGELOG.md
+1
-0
QueryBuilder.php
extensions/sphinx/QueryBuilder.php
+3
-0
CHANGELOG.md
framework/CHANGELOG.md
+1
-1
QueryBuilder.php
framework/db/QueryBuilder.php
+3
-0
QueryBuilderTest.php
tests/unit/framework/db/QueryBuilderTest.php
+16
-0
No files found.
docs/guide/db-query-builder.md
View file @
5829020e
...
@@ -85,6 +85,15 @@ When specifying columns, you may include the table prefixes or column aliases, e
...
@@ -85,6 +85,15 @@ When specifying columns, you may include the table prefixes or column aliases, e
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 array 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']`
.
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,
```
php
$subQuery
=
(
new
Query
)
->
select
(
'COUNT(*)'
)
->
from
(
'user'
);
$query
=
(
new
Query
)
->
select
([
'id'
,
'count'
=>
$subQuery
])
->
from
(
'post'
);
// $query represents the following SQL:
// SELECT `id`, (SELECT COUNT(*) FROM `user`) AS `count` FROM `post`
```
To select distinct rows, you may call
`distinct()`
, like the following:
To select distinct rows, you may call
`distinct()`
, like the following:
```
php
```
php
...
...
extensions/sphinx/CHANGELOG.md
View file @
5829020e
...
@@ -5,6 +5,7 @@ Yii Framework 2 sphinx extension Change Log
...
@@ -5,6 +5,7 @@ Yii Framework 2 sphinx extension Change Log
-----------------------
-----------------------
-
Bug #5601: Simple conditions in Query::where() and ActiveQuery::where() did not allow
`yii\db\Expression`
to be used as the value (cebe, stevekr)
-
Bug #5601: Simple conditions in Query::where() and ActiveQuery::where() did not allow
`yii\db\Expression`
to be used as the value (cebe, stevekr)
-
Enh #5223: Query builder now supports selecting sub-queries as columns (qiangxue)
2.0.0 October 12, 2014
2.0.0 October 12, 2014
...
...
extensions/sphinx/QueryBuilder.php
View file @
5829020e
...
@@ -446,6 +446,9 @@ class QueryBuilder extends Object
...
@@ -446,6 +446,9 @@ class QueryBuilder extends Object
if
(
$column
instanceof
Expression
)
{
if
(
$column
instanceof
Expression
)
{
$columns
[
$i
]
=
$column
->
expression
;
$columns
[
$i
]
=
$column
->
expression
;
$params
=
array_merge
(
$params
,
$column
->
params
);
$params
=
array_merge
(
$params
,
$column
->
params
);
}
elseif
(
$column
instanceof
Query
)
{
list
(
$sql
,
$params
)
=
$this
->
build
(
$column
,
$params
);
$columns
[
$i
]
=
"(
$sql
) AS "
.
$this
->
db
->
quoteColumnName
(
$i
);
}
elseif
(
is_string
(
$i
))
{
}
elseif
(
is_string
(
$i
))
{
if
(
strpos
(
$column
,
'('
)
===
false
)
{
if
(
strpos
(
$column
,
'('
)
===
false
)
{
$column
=
$this
->
db
->
quoteColumnName
(
$column
);
$column
=
$this
->
db
->
quoteColumnName
(
$column
);
...
...
framework/CHANGELOG.md
View file @
5829020e
...
@@ -8,13 +8,13 @@ Yii Framework 2 Change Log
...
@@ -8,13 +8,13 @@ Yii Framework 2 Change Log
-
Bug #5584:
`yii\rbac\DbRbacManager`
should not delete items when deleting a rule on a database not supporting cascade update (mdmunir)
-
Bug #5584:
`yii\rbac\DbRbacManager`
should not delete items when deleting a rule on a database not supporting cascade update (mdmunir)
-
Bug #5601: Simple conditions in Query::where() and ActiveQuery::where() did not allow
`yii\db\Expression`
to be used as the value (cebe, stevekr)
-
Bug #5601: Simple conditions in Query::where() and ActiveQuery::where() did not allow
`yii\db\Expression`
to be used as the value (cebe, stevekr)
-
Bug: Gii console command help information does not contain global options (qiangxue)
-
Bug: Gii console command help information does not contain global options (qiangxue)
-
Enh #5223: Query builder now supports selecting sub-queries as columns (qiangxue)
-
Enh #5587:
`json_encode`
is now used with
`JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`
where it makes sense, also
-
Enh #5587:
`json_encode`
is now used with
`JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`
where it makes sense, also
it is now default for
`Json::encode()`
(samdark)
it is now default for
`Json::encode()`
(samdark)
-
Enh #5600: Allow configuring debug panels in
`yii\debug\Module::panels`
as panel class name strings (qiangxue)
-
Enh #5600: Allow configuring debug panels in
`yii\debug\Module::panels`
as panel class name strings (qiangxue)
-
Enh #5613: Added
`--overwrite`
option to Gii console command to support overwriting all files (motin, qiangxue)
-
Enh #5613: Added
`--overwrite`
option to Gii console command to support overwriting all files (motin, qiangxue)
-
Enh #5646: Call
`yii\base\ErrorHandler::unregister()`
instead of
`restore_*_handlers`
directly (aivus)
-
Enh #5646: Call
`yii\base\ErrorHandler::unregister()`
instead of
`restore_*_handlers`
directly (aivus)
2.
0.0 October 12, 2014
2.
0.0 October 12, 2014
----------------------
----------------------
...
...
framework/db/QueryBuilder.php
View file @
5829020e
...
@@ -626,6 +626,9 @@ class QueryBuilder extends \yii\base\Object
...
@@ -626,6 +626,9 @@ class QueryBuilder extends \yii\base\Object
if
(
$column
instanceof
Expression
)
{
if
(
$column
instanceof
Expression
)
{
$columns
[
$i
]
=
$column
->
expression
;
$columns
[
$i
]
=
$column
->
expression
;
$params
=
array_merge
(
$params
,
$column
->
params
);
$params
=
array_merge
(
$params
,
$column
->
params
);
}
elseif
(
$column
instanceof
Query
)
{
list
(
$sql
,
$params
)
=
$this
->
build
(
$column
,
$params
);
$columns
[
$i
]
=
"(
$sql
) AS "
.
$this
->
db
->
quoteColumnName
(
$i
);
}
elseif
(
is_string
(
$i
))
{
}
elseif
(
is_string
(
$i
))
{
if
(
strpos
(
$column
,
'('
)
===
false
)
{
if
(
strpos
(
$column
,
'('
)
===
false
)
{
$column
=
$this
->
db
->
quoteColumnName
(
$column
);
$column
=
$this
->
db
->
quoteColumnName
(
$column
);
...
...
tests/unit/framework/db/QueryBuilderTest.php
View file @
5829020e
...
@@ -422,4 +422,20 @@ class QueryBuilderTest extends DatabaseTestCase
...
@@ -422,4 +422,20 @@ class QueryBuilderTest extends DatabaseTestCase
list
(
$actualQuerySql
,
$queryParams
)
=
$this
->
getQueryBuilder
()
->
build
(
$query
);
list
(
$actualQuerySql
,
$queryParams
)
=
$this
->
getQueryBuilder
()
->
build
(
$query
);
$this
->
assertEquals
(
$expectedQuerySql
,
$actualQuerySql
);
$this
->
assertEquals
(
$expectedQuerySql
,
$actualQuerySql
);
}
}
public
function
testSelectSubquery
()
{
$subquery
=
(
new
Query
())
->
select
(
'COUNT(*)'
)
->
from
(
'operations'
)
->
where
(
'account_id = accounts.id'
);
$query
=
(
new
Query
())
->
select
(
'*'
)
->
from
(
'accounts'
)
->
addSelect
([
'operations_count'
=>
$subquery
]);
list
(
$sql
,
$params
)
=
$this
->
getQueryBuilder
()
->
build
(
$query
);
$expected
=
'SELECT *, (SELECT COUNT(*) FROM `operations` WHERE account_id = accounts.id) AS `operations_count` FROM `accounts`'
;
$this
->
assertEquals
(
$expected
,
$sql
);
$this
->
assertEmpty
(
$params
);
}
}
}
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