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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
089eeaa5
Commit
089eeaa5
authored
Dec 17, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1560 from cebe/1545-query-reuse
make Query reuseable
parents
b59b77cd
b2d91669
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
14 deletions
+33
-14
ActiveQuery.php
framework/yii/db/ActiveQuery.php
+11
-2
Query.php
framework/yii/db/Query.php
+22
-12
No files found.
framework/yii/db/ActiveQuery.php
View file @
089eeaa5
...
...
@@ -123,6 +123,9 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
if
(
$this
->
sql
===
null
)
{
$select
=
$this
->
select
;
$from
=
$this
->
from
;
if
(
$this
->
from
===
null
)
{
$tableName
=
$modelClass
::
tableName
();
if
(
$this
->
select
===
null
&&
!
empty
(
$this
->
join
))
{
...
...
@@ -130,8 +133,14 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
$this
->
from
=
[
$tableName
];
}
list
(
$this
->
sql
,
$this
->
params
)
=
$db
->
getQueryBuilder
()
->
build
(
$this
);
list
(
$sql
,
$params
)
=
$db
->
getQueryBuilder
()
->
build
(
$this
);
$this
->
select
=
$select
;
$this
->
from
=
$from
;
}
else
{
$sql
=
$this
->
sql
;
$params
=
$this
->
params
;
}
return
$db
->
createCommand
(
$
this
->
sql
,
$this
->
params
);
return
$db
->
createCommand
(
$
sql
,
$
params
);
}
}
framework/yii/db/Query.php
View file @
089eeaa5
...
...
@@ -190,8 +190,7 @@ class Query extends Component implements QueryInterface
*/
public
function
count
(
$q
=
'*'
,
$db
=
null
)
{
$this
->
select
=
[
"COUNT(
$q
)"
];
return
$this
->
createCommand
(
$db
)
->
queryScalar
();
return
$this
->
queryScalar
(
"COUNT(
$q
)"
,
$db
);
}
/**
...
...
@@ -204,8 +203,7 @@ class Query extends Component implements QueryInterface
*/
public
function
sum
(
$q
,
$db
=
null
)
{
$this
->
select
=
[
"SUM(
$q
)"
];
return
$this
->
createCommand
(
$db
)
->
queryScalar
();
return
$this
->
queryScalar
(
"SUM(
$q
)"
,
$db
);
}
/**
...
...
@@ -218,8 +216,7 @@ class Query extends Component implements QueryInterface
*/
public
function
average
(
$q
,
$db
=
null
)
{
$this
->
select
=
[
"AVG(
$q
)"
];
return
$this
->
createCommand
(
$db
)
->
queryScalar
();
return
$this
->
queryScalar
(
"AVG(
$q
)"
,
$db
);
}
/**
...
...
@@ -232,8 +229,7 @@ class Query extends Component implements QueryInterface
*/
public
function
min
(
$q
,
$db
=
null
)
{
$this
->
select
=
[
"MIN(
$q
)"
];
return
$this
->
createCommand
(
$db
)
->
queryScalar
();
return
$this
->
queryScalar
(
"MIN(
$q
)"
,
$db
);
}
/**
...
...
@@ -246,8 +242,7 @@ class Query extends Component implements QueryInterface
*/
public
function
max
(
$q
,
$db
=
null
)
{
$this
->
select
=
[
"MAX(
$q
)"
];
return
$this
->
createCommand
(
$db
)
->
queryScalar
();
return
$this
->
queryScalar
(
"MAX(
$q
)"
,
$db
);
}
/**
...
...
@@ -258,8 +253,23 @@ class Query extends Component implements QueryInterface
*/
public
function
exists
(
$db
=
null
)
{
$this
->
select
=
[
new
Expression
(
'1'
)];
return
$this
->
scalar
(
$db
)
!==
false
;
return
$this
->
queryScalar
(
new
Expression
(
'1'
),
$db
)
!==
false
;
}
/**
* Queries a scalar value by setting [[select]] first.
* Restores the value of select to make this query reusable.
* @param string|Expression $selectExpression
* @param Connection $db
* @return bool|string
*/
private
function
queryScalar
(
$selectExpression
,
$db
)
{
$select
=
$this
->
select
;
$this
->
select
=
[
$selectExpression
];
$command
=
$this
->
createCommand
(
$db
);
$this
->
select
=
$select
;
return
$command
->
queryScalar
();
}
/**
...
...
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