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
39667aa6
Commit
39667aa6
authored
Mar 26, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed activequery select issue.
parent
5567caf4
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
15 deletions
+45
-15
QueryBuilder.php
framework/db/QueryBuilder.php
+39
-12
QueryBuilder.php
framework/db/oci/QueryBuilder.php
+5
-2
ActiveRecordTest.php
tests/unit/framework/db/ActiveRecordTest.php
+1
-1
No files found.
framework/db/QueryBuilder.php
View file @
39667aa6
...
...
@@ -65,18 +65,7 @@ class QueryBuilder extends \yii\base\Object
public
function
build
(
$query
,
$params
=
[])
{
$params
=
empty
(
$params
)
?
$query
->
params
:
array_merge
(
$params
,
$query
->
params
);
$select
=
$query
->
select
;
$from
=
$query
->
from
;
if
(
$from
===
null
&&
$query
instanceof
ActiveQuery
)
{
/** @var ActiveRecord $modelClass */
$modelClass
=
$query
->
modelClass
;
$tableName
=
$modelClass
::
tableName
();
$from
=
[
$tableName
];
if
(
$select
===
null
&&
!
empty
(
$query
->
join
))
{
$select
=
[
"
$tableName
.*"
];
}
}
list
(
$select
,
$from
)
=
$this
->
adjustSelectFrom
(
$query
);
$clauses
=
[
$this
->
buildSelect
(
$select
,
$params
,
$query
->
distinct
,
$query
->
selectOption
),
...
...
@@ -100,6 +89,44 @@ class QueryBuilder extends \yii\base\Object
}
/**
* Adjusts the select and from parts of the query when it is an ActiveQuery.
* When ActiveQuery does not specify "from", or if it is a join query without explicit "select",
* certain adjustments need to be made. This method is put here so that QueryBuilder can
* support sub-queries.
* @param Query $query
* @return array the select and from parts.
*/
protected
function
adjustSelectFrom
(
$query
)
{
$select
=
$query
->
select
;
$from
=
$query
->
from
;
if
(
$query
instanceof
ActiveQuery
&&
(
empty
(
$select
)
||
empty
(
$from
)))
{
/** @var ActiveRecord $modelClass */
$modelClass
=
$query
->
modelClass
;
$tableName
=
$modelClass
::
tableName
();
if
(
empty
(
$from
))
{
$from
=
[
$tableName
];
}
if
(
empty
(
$select
)
&&
!
empty
(
$query
->
join
))
{
foreach
((
array
)
$from
as
$alias
=>
$table
)
{
if
(
is_string
(
$alias
))
{
$select
=
[
"
$alias
.*"
];
}
elseif
(
is_string
(
$table
))
{
if
(
preg_match
(
'/^(.*?)\s+({{\w+}}|\w+)$/'
,
$table
,
$matches
))
{
$alias
=
$matches
[
2
];
}
else
{
$alias
=
$tableName
;
}
$select
=
[
"
$alias
.*"
];
}
break
;
}
}
}
return
[
$select
,
$from
];
}
/**
* Creates an INSERT SQL statement.
* For example,
*
...
...
framework/db/oci/QueryBuilder.php
View file @
39667aa6
...
...
@@ -8,6 +8,8 @@
namespace
yii\db\oci
;
use
yii\base\InvalidParamException
;
use
yii\db\ActiveQuery
;
use
yii\db\ActiveRecord
;
/**
* QueryBuilder is the query builder for Oracle databases.
...
...
@@ -26,10 +28,11 @@ class QueryBuilder extends \yii\db\QueryBuilder
public
function
build
(
$query
,
$params
=
[])
{
$params
=
empty
(
$params
)
?
$query
->
params
:
array_merge
(
$params
,
$query
->
params
);
list
(
$select
,
$from
)
=
$this
->
adjustSelectFrom
(
$query
);
$clauses
=
[
$this
->
buildSelect
(
$
query
->
select
,
$params
,
$query
->
distinct
,
$query
->
selectOption
),
$this
->
buildFrom
(
$
query
->
from
,
$params
),
$this
->
buildSelect
(
$select
,
$params
,
$query
->
distinct
,
$query
->
selectOption
),
$this
->
buildFrom
(
$from
,
$params
),
$this
->
buildJoin
(
$query
->
join
,
$params
),
$this
->
buildWhere
(
$query
->
where
,
$params
),
$this
->
buildGroupBy
(
$query
->
groupBy
),
...
...
tests/unit/framework/db/ActiveRecordTest.php
View file @
39667aa6
...
...
@@ -413,7 +413,7 @@ class ActiveRecordTest extends DatabaseTestCase
'items'
=>
function
(
$q
)
{
$q
->
from
([
'items'
=>
'tbl_item'
]);
},
])
->
one
();
])
->
o
rderBy
(
'tbl_order.id'
)
->
o
ne
();
}
public
function
testJoinWithAndScope
()
...
...
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