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
f62d0622
Commit
f62d0622
authored
Jan 28, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
8a5a9ce9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
87 deletions
+82
-87
ActiveQuery.php
framework/db/ar/ActiveQuery.php
+79
-84
ActiveRecord.php
framework/db/ar/ActiveRecord.php
+3
-3
No files found.
framework/db/ar/ActiveQuery.php
View file @
f62d0622
...
...
@@ -29,25 +29,26 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
public
$indexBy
;
public
$asArray
;
private
$_count
;
private
$_sql
;
private
$_countSql
;
private
$_records
;
public
$records
;
public
$sql
;
public
function
all
()
public
function
all
(
$refresh
=
false
)
{
return
$this
->
performQuery
();
if
(
$this
->
records
===
null
||
$refresh
)
{
$this
->
records
=
$this
->
performQuery
();
}
return
$this
->
records
;
}
public
function
one
()
public
function
one
(
$refresh
=
false
)
{
$this
->
limit
=
1
;
$records
=
$this
->
performQuery
();
if
(
isset
(
$records
[
0
]))
{
$this
->
_count
=
1
;
return
$records
[
0
];
if
(
$this
->
records
===
null
||
$refresh
)
{
$this
->
limit
=
1
;
$this
->
records
=
$this
->
performQuery
();
}
if
(
isset
(
$this
->
records
[
0
]))
{
return
$this
->
records
[
0
];
}
else
{
$this
->
_count
=
0
;
return
null
;
}
}
...
...
@@ -76,45 +77,15 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
return
$this
;
}
protected
function
performQuery
()
/**
* Returns the database connection used by this query.
* This method returns the connection used by the [[modelClass]].
* @return \yii\db\dao\Connection the database connection used by this query
*/
public
function
getDbConnection
()
{
$class
=
$this
->
modelClass
;
$db
=
$class
::
getDbConnection
();
$this
->
_sql
=
$this
->
getSql
(
$db
);
$command
=
$db
->
createCommand
(
$this
->
_sql
);
$command
->
bindValues
(
$this
->
params
);
$rows
=
$command
->
queryAll
();
if
(
$this
->
_asArray
)
{
$records
=
$rows
;
}
else
{
$records
=
array
();
foreach
(
$rows
as
$row
)
{
$records
[]
=
$class
::
populateRecord
(
$row
);
}
}
$this
->
_count
=
count
(
$records
);
return
$records
;
}
//
// public function getSql($connection = null)
// {
//
// }
public
function
setSql
(
$value
)
{
$this
->
_sql
=
$value
;
}
public
function
getCountSql
()
{
}
public
function
getOneSql
()
{
return
$class
::
getDbConnection
();
}
/**
...
...
@@ -123,23 +94,7 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
*/
public
function
getCount
()
{
if
(
$this
->
_count
!==
null
)
{
return
$this
->
_count
;
}
else
{
return
$this
->
_count
=
$this
->
performCountQuery
();
}
}
protected
function
performCountQuery
()
{
$select
=
$this
->
select
;
$this
->
select
=
'COUNT(*)'
;
$class
=
$this
->
modelClass
;
$command
=
$this
->
createCommand
(
$class
::
getDbConnection
());
$this
->
_countSql
=
$command
->
getSql
();
$count
=
$command
->
queryScalar
();
$this
->
select
=
$select
;
return
$count
;
return
$this
->
count
();
}
/**
...
...
@@ -155,7 +110,7 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
*/
public
function
cache
(
$duration
,
$dependency
=
null
,
$queryCount
=
1
)
{
$this
->
connection
->
cache
(
$duration
,
$dependency
,
$queryCount
);
$this
->
getDbConnection
()
->
cache
(
$duration
,
$dependency
,
$queryCount
);
return
$this
;
}
...
...
@@ -167,19 +122,30 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
*/
public
function
getIterator
()
{
$records
=
$this
->
performQuery
();
return
new
VectorIterator
(
$records
);
if
(
$this
->
records
===
null
)
{
$this
->
records
=
$this
->
performQuery
();
}
return
new
VectorIterator
(
$this
->
records
);
}
/**
* Returns the number of items in the vector.
* This method is required by the SPL `Countable` interface.
* It will be implicitly called when you use `count($vector)`.
* @param boolean $bySql whether to get the count by performing a SQL COUNT query.
* If this is false, it will count the number of records brought back by this query.
* @return integer number of items in the vector.
*/
public
function
count
()
public
function
count
(
$bySql
=
false
)
{
return
$this
->
getCount
();
if
(
$bySql
)
{
return
$this
->
performCountQuery
();
}
else
{
if
(
$this
->
records
===
null
)
{
$this
->
records
=
$this
->
performQuery
();
}
return
count
(
$this
->
records
);
}
}
/**
...
...
@@ -191,10 +157,10 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
*/
public
function
offsetExists
(
$offset
)
{
if
(
$this
->
_
records
===
null
)
{
$this
->
_
records
=
$this
->
performQuery
();
if
(
$this
->
records
===
null
)
{
$this
->
records
=
$this
->
performQuery
();
}
return
isset
(
$this
->
_
records
[
$offset
]);
return
isset
(
$this
->
records
[
$offset
]);
}
/**
...
...
@@ -208,10 +174,10 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
*/
public
function
offsetGet
(
$offset
)
{
if
(
$this
->
_
records
===
null
)
{
$this
->
_
records
=
$this
->
performQuery
();
if
(
$this
->
records
===
null
)
{
$this
->
records
=
$this
->
performQuery
();
}
return
isset
(
$this
->
_records
[
$offset
])
?
$this
->
_
records
[
$offset
]
:
null
;
return
isset
(
$this
->
records
[
$offset
])
?
$this
->
records
[
$offset
]
:
null
;
}
/**
...
...
@@ -227,10 +193,10 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
*/
public
function
offsetSet
(
$offset
,
$item
)
{
if
(
$this
->
_
records
===
null
)
{
$this
->
_
records
=
$this
->
performQuery
();
if
(
$this
->
records
===
null
)
{
$this
->
records
=
$this
->
performQuery
();
}
$this
->
_
records
[
$offset
]
=
$item
;
$this
->
records
[
$offset
]
=
$item
;
}
/**
...
...
@@ -243,9 +209,38 @@ class ActiveQuery extends BaseQuery implements \IteratorAggregate, \ArrayAccess,
*/
public
function
offsetUnset
(
$offset
)
{
if
(
$this
->
_
records
===
null
)
{
$this
->
_
records
=
$this
->
performQuery
();
if
(
$this
->
records
===
null
)
{
$this
->
records
=
$this
->
performQuery
();
}
unset
(
$this
->
_records
[
$offset
]);
unset
(
$this
->
records
[
$offset
]);
}
protected
function
performQuery
()
{
$db
=
$this
->
getDbConnection
();
$this
->
sql
=
$this
->
getSql
(
$db
);
$command
=
$db
->
createCommand
(
$this
->
sql
);
$command
->
bindValues
(
$this
->
params
);
$rows
=
$command
->
queryAll
();
if
(
$this
->
asArray
)
{
$records
=
$rows
;
}
else
{
$records
=
array
();
$class
=
$this
->
modelClass
;
foreach
(
$rows
as
$row
)
{
$records
[]
=
$class
::
populateData
(
$row
);
}
}
return
$records
;
}
protected
function
performCountQuery
()
{
$this
->
select
=
'COUNT(*)'
;
$class
=
$this
->
modelClass
;
$command
=
$this
->
createCommand
(
$class
::
getDbConnection
());
$this
->
sql
=
$command
->
getSql
();
$count
=
$command
->
queryScalar
();
return
$count
;
}
}
framework/db/ar/ActiveRecord.php
View file @
f62d0622
...
...
@@ -308,7 +308,7 @@ abstract class ActiveRecord extends \yii\base\Model
*/
public
function
__construct
(
$scenario
=
'insert'
)
{
if
(
$scenario
===
null
)
// internally used by populate
Record
() and model()
if
(
$scenario
===
null
)
// internally used by populate
Data
() and model()
{
return
;
}
...
...
@@ -1186,7 +1186,7 @@ abstract class ActiveRecord extends \yii\base\Model
* @return ActiveRecord the newly created active record. The class of the object is the same as the model class.
* Null is returned if the input data is false.
*/
public
static
function
populate
Record
(
$row
)
public
static
function
populate
Data
(
$row
)
{
$record
=
static
::
instantiate
(
$row
);
$record
->
setScenario
(
'update'
);
...
...
@@ -1204,7 +1204,7 @@ abstract class ActiveRecord extends \yii\base\Model
/**
* Creates an active record instance.
* This method is called by {@link populate
Record} and {@link populateRecords
}.
* This method is called by {@link populate
Data
}.
* You may override this method if the instance being created
* depends the attributes that are to be populated to the record.
* For example, by creating a record based on the value of a column,
...
...
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