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
27238f8d
Commit
27238f8d
authored
Aug 03, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored ActiveDataProvider.
parent
250ec28e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
18 deletions
+36
-18
ActiveDataProvider.php
framework/yii/data/ActiveDataProvider.php
+21
-18
ActiveDataProviderTest.php
tests/unit/framework/data/ActiveDataProviderTest.php
+15
-0
No files found.
framework/yii/data/ActiveDataProvider.php
View file @
27238f8d
...
@@ -111,15 +111,14 @@ class ActiveDataProvider extends DataProvider
...
@@ -111,15 +111,14 @@ class ActiveDataProvider extends DataProvider
* When [[pagination]] is false, this returns the same value as [[count]].
* When [[pagination]] is false, this returns the same value as [[count]].
* If [[totalCount]] is not explicitly set, it will be calculated
* If [[totalCount]] is not explicitly set, it will be calculated
* using [[query]] with a COUNT query.
* using [[query]] with a COUNT query.
* @param boolean $refresh whether to recalculate the model count
* @return integer total number of possible data models.
* @return integer total number of possible data models.
* @throws InvalidConfigException
* @throws InvalidConfigException
*/
*/
public
function
getTotalCount
(
$refresh
=
false
)
public
function
getTotalCount
()
{
{
if
(
$this
->
getPagination
()
===
false
)
{
if
(
$this
->
getPagination
()
===
false
)
{
return
$this
->
getCount
();
return
$this
->
getCount
();
}
elseif
(
$this
->
_totalCount
===
null
||
$refresh
)
{
}
elseif
(
$this
->
_totalCount
===
null
)
{
if
(
!
$this
->
query
instanceof
Query
)
{
if
(
!
$this
->
query
instanceof
Query
)
{
throw
new
InvalidConfigException
(
'The "query" property must be an instance of Query or its subclass.'
);
throw
new
InvalidConfigException
(
'The "query" property must be an instance of Query or its subclass.'
);
}
}
...
@@ -141,11 +140,22 @@ class ActiveDataProvider extends DataProvider
...
@@ -141,11 +140,22 @@ class ActiveDataProvider extends DataProvider
/**
/**
* Returns the data models in the current page.
* Returns the data models in the current page.
* @return array the list of data models in the current page.
* @return array the list of data models in the current page.
* @throws InvalidConfigException if [[query]] is not set or invalid.
*/
*/
public
function
getModels
()
public
function
getModels
()
{
{
if
(
$this
->
_models
===
null
)
{
if
(
$this
->
_models
===
null
)
{
$this
->
loadModels
();
if
(
!
$this
->
query
instanceof
Query
)
{
throw
new
InvalidConfigException
(
'The "query" property must be an instance of Query or its subclass.'
);
}
if
((
$pagination
=
$this
->
getPagination
())
!==
false
)
{
$pagination
->
totalCount
=
$this
->
getTotalCount
();
$this
->
query
->
limit
(
$pagination
->
getLimit
())
->
offset
(
$pagination
->
getOffset
());
}
if
((
$sort
=
$this
->
getSort
())
!==
false
)
{
$this
->
query
->
orderBy
(
$sort
->
getOrders
());
}
$this
->
_models
=
$this
->
query
->
all
(
$this
->
db
);
}
}
return
$this
->
_models
;
return
$this
->
_models
;
}
}
...
@@ -194,21 +204,14 @@ class ActiveDataProvider extends DataProvider
...
@@ -194,21 +204,14 @@ class ActiveDataProvider extends DataProvider
}
}
/**
/**
* Performs query and load data models.
* Refreshes the data provider.
* @throws InvalidConfigException if [[query]] is not set or invalid.
* After calling this method, if [[getModels()]], [[getKeys()]] or [[getTotalCount()]] is called again,
* they will re-execute the query and return the latest data available.
*/
*/
public
function
loadModels
()
public
function
refresh
()
{
{
if
(
!
$this
->
query
instanceof
Query
)
{
$this
->
_models
=
null
;
throw
new
InvalidConfigException
(
'The "query" property must be an instance of Query or its subclass.'
);
$this
->
_totalCount
=
null
;
}
$this
->
_keys
=
null
;
if
((
$pagination
=
$this
->
getPagination
())
!==
false
)
{
$pagination
->
totalCount
=
$this
->
getTotalCount
();
$this
->
query
->
limit
(
$pagination
->
getLimit
())
->
offset
(
$pagination
->
getOffset
());
}
if
((
$sort
=
$this
->
getSort
())
!==
false
)
{
$this
->
query
->
orderBy
(
$sort
->
getOrders
());
}
$this
->
_models
=
$this
->
query
->
all
(
$this
->
db
);
}
}
}
}
tests/unit/framework/data/ActiveDataProviderTest.php
View file @
27238f8d
...
@@ -68,4 +68,19 @@ class ActiveDataProviderTest extends DatabaseTestCase
...
@@ -68,4 +68,19 @@ class ActiveDataProviderTest extends DatabaseTestCase
$orders
=
$provider
->
getModels
();
$orders
=
$provider
->
getModels
();
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
count
(
$orders
));
}
}
public
function
testRefresh
()
{
$query
=
new
Query
;
$provider
=
new
ActiveDataProvider
(
array
(
'db'
=>
$this
->
getConnection
(),
'query'
=>
$query
->
from
(
'tbl_order'
)
->
orderBy
(
'id'
),
));
$this
->
assertEquals
(
3
,
count
(
$provider
->
getModels
()));
$provider
->
getPagination
()
->
pageSize
=
2
;
$this
->
assertEquals
(
3
,
count
(
$provider
->
getModels
()));
$provider
->
refresh
();
$this
->
assertEquals
(
2
,
count
(
$provider
->
getModels
()));
}
}
}
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