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
b31b02a5
Commit
b31b02a5
authored
Aug 03, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
renamed IDataProvider::getItems() to getModels()
parent
3d5388ff
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
153 additions
and
160 deletions
+153
-160
ActiveDataProvider.php
framework/yii/data/ActiveDataProvider.php
+43
-43
ArrayDataProvider.php
framework/yii/data/ArrayDataProvider.php
+45
-51
DataProvider.php
framework/yii/data/DataProvider.php
+3
-3
IDataProvider.php
framework/yii/data/IDataProvider.php
+10
-10
GridView.php
framework/yii/widgets/GridView.php
+17
-17
ListView.php
framework/yii/widgets/ListView.php
+14
-14
ListViewBase.php
framework/yii/widgets/ListViewBase.php
+1
-1
Column.php
framework/yii/widgets/grid/Column.php
+8
-8
DataColumn.php
framework/yii/widgets/grid/DataColumn.php
+8
-9
ActiveDataProviderTest.php
tests/unit/framework/data/ActiveDataProviderTest.php
+4
-4
No files found.
framework/yii/data/ActiveDataProvider.php
View file @
b31b02a5
...
@@ -29,7 +29,7 @@ use yii\db\Connection;
...
@@ -29,7 +29,7 @@ use yii\db\Connection;
* ));
* ));
*
*
* // get the posts in the current page
* // get the posts in the current page
* $posts = $provider->get
Item
s();
* $posts = $provider->get
Model
s();
* ~~~
* ~~~
*
*
* And the following example shows how to use ActiveDataProvider without ActiveRecord:
* And the following example shows how to use ActiveDataProvider without ActiveRecord:
...
@@ -44,7 +44,7 @@ use yii\db\Connection;
...
@@ -44,7 +44,7 @@ use yii\db\Connection;
* ));
* ));
*
*
* // get the posts in the current page
* // get the posts in the current page
* $posts = $provider->get
Item
s();
* $posts = $provider->get
Model
s();
* ~~~
* ~~~
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
...
@@ -53,18 +53,18 @@ use yii\db\Connection;
...
@@ -53,18 +53,18 @@ use yii\db\Connection;
class
ActiveDataProvider
extends
DataProvider
class
ActiveDataProvider
extends
DataProvider
{
{
/**
/**
* @var Query the query that is used to fetch data
item
s and [[totalCount]]
* @var Query the query that is used to fetch data
model
s and [[totalCount]]
* if it is not explicitly set.
* if it is not explicitly set.
*/
*/
public
$query
;
public
$query
;
/**
/**
* @var string|callable the column that is used as the key of the data
item
s.
* @var string|callable the column that is used as the key of the data
model
s.
* This can be either a column name, or a callable that returns the key value of a given data
item
.
* This can be either a column name, or a callable that returns the key value of a given data
model
.
*
*
* If this is not set, the following rules will be used to determine the keys of the data
item
s:
* If this is not set, the following rules will be used to determine the keys of the data
model
s:
*
*
* - If [[query]] is an [[ActiveQuery]] instance, the primary keys of [[ActiveQuery::modelClass]] will be used.
* - If [[query]] is an [[ActiveQuery]] instance, the primary keys of [[ActiveQuery::modelClass]] will be used.
* - Otherwise, the keys of the [[
item
s]] array will be used.
* - Otherwise, the keys of the [[
model
s]] array will be used.
*
*
* @see getKeys()
* @see getKeys()
*/
*/
...
@@ -75,9 +75,9 @@ class ActiveDataProvider extends DataProvider
...
@@ -75,9 +75,9 @@ class ActiveDataProvider extends DataProvider
*/
*/
public
$db
;
public
$db
;
private
$_
item
s
;
private
$_
model
s
;
private
$_keys
;
private
$_keys
;
private
$_
c
ount
;
private
$_
totalC
ount
;
/**
/**
* Initializes the DbCache component.
* Initializes the DbCache component.
...
@@ -96,59 +96,59 @@ class ActiveDataProvider extends DataProvider
...
@@ -96,59 +96,59 @@ class ActiveDataProvider extends DataProvider
}
}
/**
/**
* Returns the number of data
item
s in the current page.
* Returns the number of data
model
s in the current page.
* This is equivalent to `count($provider->
item
s)`.
* This is equivalent to `count($provider->
model
s)`.
* When [[pagination]] is false, this is the same as [[totalCount]].
* When [[pagination]] is false, this is the same as [[totalCount]].
* @param boolean $refresh whether to recalculate the
item
count. If true,
* @param boolean $refresh whether to recalculate the
model
count. If true,
* this will cause re-fetching of [[
item
s]].
* this will cause re-fetching of [[
model
s]].
* @return integer the number of data
item
s in the current page.
* @return integer the number of data
model
s in the current page.
*/
*/
public
function
getCount
(
$refresh
=
false
)
public
function
getCount
(
$refresh
=
false
)
{
{
return
count
(
$this
->
get
Item
s
(
$refresh
));
return
count
(
$this
->
get
Model
s
(
$refresh
));
}
}
/**
/**
* Returns the total number of data
item
s.
* Returns the total number of data
model
s.
* 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
item
count
* @param boolean $refresh whether to recalculate the
model
count
* @return integer total number of possible data
item
s.
* @return integer total number of possible data
model
s.
* @throws InvalidConfigException
* @throws InvalidConfigException
*/
*/
public
function
getTotalCount
(
$refresh
=
false
)
public
function
getTotalCount
(
$refresh
=
false
)
{
{
if
(
$this
->
getPagination
()
===
false
)
{
if
(
$this
->
getPagination
()
===
false
)
{
return
$this
->
getCount
(
$refresh
);
return
$this
->
getCount
(
$refresh
);
}
elseif
(
$this
->
_
c
ount
===
null
||
$refresh
)
{
}
elseif
(
$this
->
_
totalC
ount
===
null
||
$refresh
)
{
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.'
);
}
}
$query
=
clone
$this
->
query
;
$query
=
clone
$this
->
query
;
$this
->
_
c
ount
=
$query
->
limit
(
-
1
)
->
offset
(
-
1
)
->
count
(
'*'
,
$this
->
db
);
$this
->
_
totalC
ount
=
$query
->
limit
(
-
1
)
->
offset
(
-
1
)
->
count
(
'*'
,
$this
->
db
);
}
}
return
$this
->
_
c
ount
;
return
$this
->
_
totalC
ount
;
}
}
/**
/**
* Sets the total number of data
item
s.
* Sets the total number of data
model
s.
* @param integer $value the total number of data
item
s.
* @param integer $value the total number of data
model
s.
*/
*/
public
function
setTotalCount
(
$value
)
public
function
setTotalCount
(
$value
)
{
{
$this
->
_
c
ount
=
$value
;
$this
->
_
totalC
ount
=
$value
;
}
}
/**
/**
* Returns the data
item
s in the current page.
* Returns the data
model
s in the current page.
* @param boolean $refresh whether to re-fetch the data
item
s.
* @param boolean $refresh whether to re-fetch the data
model
s.
* @return array the list of data
item
s in the current page.
* @return array the list of data
model
s in the current page.
* @throws InvalidConfigException
* @throws InvalidConfigException
*/
*/
public
function
get
Item
s
(
$refresh
=
false
)
public
function
get
Model
s
(
$refresh
=
false
)
{
{
if
(
$this
->
_
item
s
===
null
||
$refresh
)
{
if
(
$this
->
_
model
s
===
null
||
$refresh
)
{
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.'
);
}
}
...
@@ -159,28 +159,28 @@ class ActiveDataProvider extends DataProvider
...
@@ -159,28 +159,28 @@ class ActiveDataProvider extends DataProvider
if
((
$sort
=
$this
->
getSort
())
!==
false
)
{
if
((
$sort
=
$this
->
getSort
())
!==
false
)
{
$this
->
query
->
orderBy
(
$sort
->
getOrders
());
$this
->
query
->
orderBy
(
$sort
->
getOrders
());
}
}
$this
->
_
item
s
=
$this
->
query
->
all
(
$this
->
db
);
$this
->
_
model
s
=
$this
->
query
->
all
(
$this
->
db
);
}
}
return
$this
->
_
item
s
;
return
$this
->
_
model
s
;
}
}
/**
/**
* Returns the key values associated with the data
item
s.
* Returns the key values associated with the data
model
s.
* @param boolean $refresh whether to re-fetch the data
item
s and re-calculate the keys
* @param boolean $refresh whether to re-fetch the data
model
s and re-calculate the keys
* @return array the list of key values corresponding to [[
items]]. Each data item in [[item
s]]
* @return array the list of key values corresponding to [[
models]]. Each data model in [[model
s]]
* is uniquely identified by the corresponding key value in this array.
* is uniquely identified by the corresponding key value in this array.
*/
*/
public
function
getKeys
(
$refresh
=
false
)
public
function
getKeys
(
$refresh
=
false
)
{
{
if
(
$this
->
_keys
===
null
||
$refresh
)
{
if
(
$this
->
_keys
===
null
||
$refresh
)
{
$this
->
_keys
=
array
();
$this
->
_keys
=
array
();
$
items
=
$this
->
getItem
s
(
$refresh
);
$
models
=
$this
->
getModel
s
(
$refresh
);
if
(
$this
->
key
!==
null
)
{
if
(
$this
->
key
!==
null
)
{
foreach
(
$
items
as
$item
)
{
foreach
(
$
models
as
$model
)
{
if
(
is_string
(
$this
->
key
))
{
if
(
is_string
(
$this
->
key
))
{
$this
->
_keys
[]
=
$
item
[
$this
->
key
];
$this
->
_keys
[]
=
$
model
[
$this
->
key
];
}
else
{
}
else
{
$this
->
_keys
[]
=
call_user_func
(
$this
->
key
,
$
item
);
$this
->
_keys
[]
=
call_user_func
(
$this
->
key
,
$
model
);
}
}
}
}
}
elseif
(
$this
->
query
instanceof
ActiveQuery
)
{
}
elseif
(
$this
->
query
instanceof
ActiveQuery
)
{
...
@@ -189,20 +189,20 @@ class ActiveDataProvider extends DataProvider
...
@@ -189,20 +189,20 @@ class ActiveDataProvider extends DataProvider
$pks
=
$class
::
primaryKey
();
$pks
=
$class
::
primaryKey
();
if
(
count
(
$pks
)
===
1
)
{
if
(
count
(
$pks
)
===
1
)
{
$pk
=
$pks
[
0
];
$pk
=
$pks
[
0
];
foreach
(
$
items
as
$item
)
{
foreach
(
$
models
as
$model
)
{
$this
->
_keys
[]
=
$
item
[
$pk
];
$this
->
_keys
[]
=
$
model
[
$pk
];
}
}
}
else
{
}
else
{
foreach
(
$
items
as
$item
)
{
foreach
(
$
models
as
$model
)
{
$keys
=
array
();
$keys
=
array
();
foreach
(
$pks
as
$pk
)
{
foreach
(
$pks
as
$pk
)
{
$keys
[]
=
$
item
[
$pk
];
$keys
[]
=
$
model
[
$pk
];
}
}
$this
->
_keys
[]
=
json_encode
(
$keys
);
$this
->
_keys
[]
=
json_encode
(
$keys
);
}
}
}
}
}
else
{
}
else
{
$this
->
_keys
=
array_keys
(
$
item
s
);
$this
->
_keys
=
array_keys
(
$
model
s
);
}
}
}
}
return
$this
->
_keys
;
return
$this
->
_keys
;
...
...
framework/yii/data/ArrayDataProvider.php
View file @
b31b02a5
...
@@ -13,25 +13,25 @@ use yii\helpers\ArrayHelper;
...
@@ -13,25 +13,25 @@ use yii\helpers\ArrayHelper;
/**
/**
* ArrayDataProvider implements a data provider based on a data array.
* ArrayDataProvider implements a data provider based on a data array.
*
*
* The [[all
Items]] property contains all data item
s that may be sorted and/or paginated.
* The [[all
Models]] property contains all data model
s that may be sorted and/or paginated.
* ArrayDataProvider will provide the data after sorting and/or pagination.
* ArrayDataProvider will provide the data after sorting and/or pagination.
* You may configure the [[sort]] and [[pagination]] properties to
* You may configure the [[sort]] and [[pagination]] properties to
* customize the sorting and pagination behaviors.
* customize the sorting and pagination behaviors.
*
*
* Elements in the [[all
Item
s]] array may be either objects (e.g. model objects)
* Elements in the [[all
Model
s]] array may be either objects (e.g. model objects)
* or associative arrays (e.g. query results of DAO).
* or associative arrays (e.g. query results of DAO).
* Make sure to set the [[key]] property to the name of the field that uniquely
* Make sure to set the [[key]] property to the name of the field that uniquely
* identifies a data record or false if you do not have such a field.
* identifies a data record or false if you do not have such a field.
*
*
* Compared to [[ActiveDataProvider]], ArrayDataProvider could be less efficient
* Compared to [[ActiveDataProvider]], ArrayDataProvider could be less efficient
* because it needs to have [[all
Item
s]] ready.
* because it needs to have [[all
Model
s]] ready.
*
*
* ArrayDataProvider may be used in the following way:
* ArrayDataProvider may be used in the following way:
*
*
* ~~~
* ~~~
* $query = new Query;
* $query = new Query;
* $provider = new ArrayDataProvider(array(
* $provider = new ArrayDataProvider(array(
* 'all
Item
s' => $query->from('tbl_post')->all(),
* 'all
Model
s' => $query->from('tbl_post')->all(),
* 'sort' => array(
* 'sort' => array(
* 'attributes' => array(
* 'attributes' => array(
* 'id', 'username', 'email',
* 'id', 'username', 'email',
...
@@ -42,7 +42,7 @@ use yii\helpers\ArrayHelper;
...
@@ -42,7 +42,7 @@ use yii\helpers\ArrayHelper;
* ),
* ),
* ));
* ));
* // get the posts in the current page
* // get the posts in the current page
* $posts = $provider->get
Item
s();
* $posts = $provider->get
Model
s();
* ~~~
* ~~~
*
*
* Note: if you want to use the sorting feature, you must configure the [[sort]] property
* Note: if you want to use the sorting feature, you must configure the [[sort]] property
...
@@ -54,116 +54,110 @@ use yii\helpers\ArrayHelper;
...
@@ -54,116 +54,110 @@ use yii\helpers\ArrayHelper;
class
ArrayDataProvider
extends
DataProvider
class
ArrayDataProvider
extends
DataProvider
{
{
/**
/**
* @var string|callable the column that is used as the key of the data
item
s.
* @var string|callable the column that is used as the key of the data
model
s.
* This can be either a column name, or a callable that returns the key value of a given data
item
.
* This can be either a column name, or a callable that returns the key value of a given data
model
.
* If this is not set, the index of the [[
item
s]] array will be used.
* If this is not set, the index of the [[
model
s]] array will be used.
* @see getKeys()
* @see getKeys()
*/
*/
public
$key
;
public
$key
;
/**
/**
* @var array the data that is not paginated or sorted. When pagination is enabled,
* @var array the data that is not paginated or sorted. When pagination is enabled,
* this property usually contains more elements than [[
item
s]].
* this property usually contains more elements than [[
model
s]].
* The array elements must use zero-based integer keys.
* The array elements must use zero-based integer keys.
*/
*/
public
$all
Item
s
;
public
$all
Model
s
;
private
$_totalCount
;
private
$_totalCount
;
/**
/**
* Returns the total number of data items.
* Returns the total number of data models.
* @return integer total number of possible data items.
* @return integer total number of possible data models.
* @throws InvalidConfigException
*/
*/
public
function
getTotalCount
()
public
function
getTotalCount
()
{
{
if
(
$this
->
getPagination
()
===
false
)
{
if
(
$this
->
getPagination
()
===
false
)
{
return
$this
->
getCount
();
return
$this
->
getCount
();
}
elseif
(
$this
->
_totalCount
===
null
)
{
}
elseif
(
$this
->
_totalCount
===
null
)
{
if
(
$this
->
allItems
!==
null
)
{
$this
->
_totalCount
=
count
(
$this
->
allModels
);
$this
->
_totalCount
=
count
(
$this
->
allItems
);
}
else
{
throw
new
InvalidConfigException
(
'Unable to determine total item count: either "allItems" or "totalCount" must be set.'
);
}
}
}
return
$this
->
_totalCount
;
return
$this
->
_totalCount
;
}
}
/**
/**
* Sets the total number of data
item
s.
* Sets the total number of data
model
s.
* @param integer $value the total number of data
item
s.
* @param integer $value the total number of data
model
s.
*/
*/
public
function
setTotalCount
(
$value
)
public
function
setTotalCount
(
$value
)
{
{
$this
->
_totalCount
=
$value
;
$this
->
_totalCount
=
$value
;
}
}
private
$_
item
s
;
private
$_
model
s
;
/**
/**
* Returns the data items in the current page.
* Returns the data models in the current page.
* @return array the list of data items in the current page.
* @return array the list of data models in the current page.
* @throws InvalidConfigException
*/
*/
public
function
get
Item
s
()
public
function
get
Model
s
()
{
{
if
(
$this
->
_
item
s
===
null
)
{
if
(
$this
->
_
model
s
===
null
)
{
if
((
$
items
=
$this
->
allItem
s
)
===
null
)
{
if
((
$
models
=
$this
->
allModel
s
)
===
null
)
{
throw
new
InvalidConfigException
(
'Either "items" or "allItems" must be set.'
);
return
array
(
);
}
}
if
((
$sort
=
$this
->
getSort
())
!==
false
)
{
if
((
$sort
=
$this
->
getSort
())
!==
false
)
{
$
items
=
$this
->
sortItems
(
$item
s
,
$sort
);
$
models
=
$this
->
sortModels
(
$model
s
,
$sort
);
}
}
if
((
$pagination
=
$this
->
getPagination
())
!==
false
)
{
if
((
$pagination
=
$this
->
getPagination
())
!==
false
)
{
$pagination
->
totalCount
=
$this
->
getTotalCount
();
$pagination
->
totalCount
=
$this
->
getTotalCount
();
$
items
=
array_slice
(
$item
s
,
$pagination
->
getOffset
(),
$pagination
->
getLimit
());
$
models
=
array_slice
(
$model
s
,
$pagination
->
getOffset
(),
$pagination
->
getLimit
());
}
}
$this
->
_
items
=
$item
s
;
$this
->
_
models
=
$model
s
;
}
}
return
$this
->
_
item
s
;
return
$this
->
_
model
s
;
}
}
/**
/**
* Sets the data
item
s in the current page.
* Sets the data
model
s in the current page.
* @param array $
items the item
s in the current page
* @param array $
models the model
s in the current page
*/
*/
public
function
set
Items
(
$item
s
)
public
function
set
Models
(
$model
s
)
{
{
$this
->
_
items
=
$item
s
;
$this
->
_
models
=
$model
s
;
}
}
private
$_keys
;
private
$_keys
;
/**
/**
* Returns the key values associated with the data
item
s.
* Returns the key values associated with the data
model
s.
* @return array the list of key values corresponding to [[
items]]. Each data item in [[item
s]]
* @return array the list of key values corresponding to [[
models]]. Each data model in [[model
s]]
* is uniquely identified by the corresponding key value in this array.
* is uniquely identified by the corresponding key value in this array.
*/
*/
public
function
getKeys
()
public
function
getKeys
()
{
{
if
(
$this
->
_keys
===
null
)
{
if
(
$this
->
_keys
===
null
)
{
$this
->
_keys
=
array
();
$this
->
_keys
=
array
();
$
items
=
$this
->
getItem
s
();
$
models
=
$this
->
getModel
s
();
if
(
$this
->
key
!==
null
)
{
if
(
$this
->
key
!==
null
)
{
foreach
(
$
items
as
$item
)
{
foreach
(
$
models
as
$model
)
{
if
(
is_string
(
$this
->
key
))
{
if
(
is_string
(
$this
->
key
))
{
$this
->
_keys
[]
=
$
item
[
$this
->
key
];
$this
->
_keys
[]
=
$
model
[
$this
->
key
];
}
else
{
}
else
{
$this
->
_keys
[]
=
call_user_func
(
$this
->
key
,
$
item
);
$this
->
_keys
[]
=
call_user_func
(
$this
->
key
,
$
model
);
}
}
}
}
}
else
{
}
else
{
$this
->
_keys
=
array_keys
(
$
item
s
);
$this
->
_keys
=
array_keys
(
$
model
s
);
}
}
}
}
return
$this
->
_keys
;
return
$this
->
_keys
;
}
}
/**
/**
* Sets the key values associated with the data
item
s.
* Sets the key values associated with the data
model
s.
* @param array $keys the list of key values corresponding to [[
item
s]].
* @param array $keys the list of key values corresponding to [[
model
s]].
*/
*/
public
function
setKeys
(
$keys
)
public
function
setKeys
(
$keys
)
{
{
...
@@ -171,17 +165,17 @@ class ArrayDataProvider extends DataProvider
...
@@ -171,17 +165,17 @@ class ArrayDataProvider extends DataProvider
}
}
/**
/**
* Sorts the data
item
s according to the given sort definition
* Sorts the data
model
s according to the given sort definition
* @param array $
items the item
s to be sorted
* @param array $
models the model
s to be sorted
* @param Sort $sort the sort definition
* @param Sort $sort the sort definition
* @return array the sorted data
item
s
* @return array the sorted data
model
s
*/
*/
protected
function
sort
Items
(
$item
s
,
$sort
)
protected
function
sort
Models
(
$model
s
,
$sort
)
{
{
$orders
=
$sort
->
getOrders
();
$orders
=
$sort
->
getOrders
();
if
(
!
empty
(
$orders
))
{
if
(
!
empty
(
$orders
))
{
ArrayHelper
::
multisort
(
$
item
s
,
array_keys
(
$orders
),
array_values
(
$orders
));
ArrayHelper
::
multisort
(
$
model
s
,
array_keys
(
$orders
),
array_values
(
$orders
));
}
}
return
$
item
s
;
return
$
model
s
;
}
}
}
}
framework/yii/data/DataProvider.php
View file @
b31b02a5
...
@@ -118,11 +118,11 @@ abstract class DataProvider extends Component implements IDataProvider
...
@@ -118,11 +118,11 @@ abstract class DataProvider extends Component implements IDataProvider
}
}
/**
/**
* Returns the number of data
item
s in the current page.
* Returns the number of data
model
s in the current page.
* @return integer the number of data
item
s in the current page.
* @return integer the number of data
model
s in the current page.
*/
*/
public
function
getCount
()
public
function
getCount
()
{
{
return
count
(
$this
->
get
Item
s
());
return
count
(
$this
->
get
Model
s
());
}
}
}
}
framework/yii/data/IDataProvider.php
View file @
b31b02a5
...
@@ -19,29 +19,29 @@ namespace yii\data;
...
@@ -19,29 +19,29 @@ namespace yii\data;
interface
IDataProvider
interface
IDataProvider
{
{
/**
/**
* Returns the number of data
item
s in the current page.
* Returns the number of data
model
s in the current page.
* This is equivalent to `count($provider->get
Item
s())`.
* This is equivalent to `count($provider->get
Model
s())`.
* When [[pagination]] is false, this is the same as [[totalCount]].
* When [[pagination]] is false, this is the same as [[totalCount]].
* @return integer the number of data
item
s in the current page.
* @return integer the number of data
model
s in the current page.
*/
*/
public
function
getCount
();
public
function
getCount
();
/**
/**
* Returns the total number of data
item
s.
* Returns the total number of data
model
s.
* When [[pagination]] is false, this is the same as [[count]].
* When [[pagination]] is false, this is the same as [[count]].
* @return integer total number of possible data
item
s.
* @return integer total number of possible data
model
s.
*/
*/
public
function
getTotalCount
();
public
function
getTotalCount
();
/**
/**
* Returns the data
item
s in the current page.
* Returns the data
model
s in the current page.
* @return array the list of data
item
s in the current page.
* @return array the list of data
model
s in the current page.
*/
*/
public
function
get
Item
s
();
public
function
get
Model
s
();
/**
/**
* Returns the key values associated with the data
item
s.
* Returns the key values associated with the data
model
s.
* @return array the list of key values corresponding to [[
items]]. Each data item in [[item
s]]
* @return array the list of key values corresponding to [[
models]]. Each data model in [[model
s]]
* is uniquely identified by the corresponding key value in this array.
* is uniquely identified by the corresponding key value in this array.
*/
*/
public
function
getKeys
();
public
function
getKeys
();
...
...
framework/yii/widgets/GridView.php
View file @
b31b02a5
...
@@ -112,7 +112,7 @@ class GridView extends ListViewBase
...
@@ -112,7 +112,7 @@ class GridView extends ListViewBase
}
}
/**
/**
* Renders the data
item
s for the grid view.
* Renders the data
model
s for the grid view.
*/
*/
public
function
renderItems
()
public
function
renderItems
()
{
{
...
@@ -217,22 +217,22 @@ class GridView extends ListViewBase
...
@@ -217,22 +217,22 @@ class GridView extends ListViewBase
*/
*/
public
function
renderTableBody
()
public
function
renderTableBody
()
{
{
$
items
=
array_values
(
$this
->
dataProvider
->
getItem
s
());
$
models
=
array_values
(
$this
->
dataProvider
->
getModel
s
());
$keys
=
$this
->
dataProvider
->
getKeys
();
$keys
=
$this
->
dataProvider
->
getKeys
();
$rows
=
array
();
$rows
=
array
();
foreach
(
$
items
as
$index
=>
$item
)
{
foreach
(
$
models
as
$index
=>
$model
)
{
$key
=
$keys
[
$index
];
$key
=
$keys
[
$index
];
if
(
$this
->
beforeRow
!==
null
)
{
if
(
$this
->
beforeRow
!==
null
)
{
$row
=
call_user_func
(
$this
->
beforeRow
,
$
item
,
$key
,
$index
);
$row
=
call_user_func
(
$this
->
beforeRow
,
$
model
,
$key
,
$index
);
if
(
!
empty
(
$row
))
{
if
(
!
empty
(
$row
))
{
$rows
[]
=
$row
;
$rows
[]
=
$row
;
}
}
}
}
$rows
[]
=
$this
->
renderTableRow
(
$
item
,
$key
,
$index
);
$rows
[]
=
$this
->
renderTableRow
(
$
model
,
$key
,
$index
);
if
(
$this
->
afterRow
!==
null
)
{
if
(
$this
->
afterRow
!==
null
)
{
$row
=
call_user_func
(
$this
->
afterRow
,
$
item
,
$key
,
$index
);
$row
=
call_user_func
(
$this
->
afterRow
,
$
model
,
$key
,
$index
);
if
(
!
empty
(
$row
))
{
if
(
!
empty
(
$row
))
{
$rows
[]
=
$row
;
$rows
[]
=
$row
;
}
}
...
@@ -242,21 +242,21 @@ class GridView extends ListViewBase
...
@@ -242,21 +242,21 @@ class GridView extends ListViewBase
}
}
/**
/**
* Renders a table row with the given data
item
and key.
* Renders a table row with the given data
model
and key.
* @param mixed $
item the data item
* @param mixed $
model the data model to be rendered
* @param mixed $key the key associated with the data
item
* @param mixed $key the key associated with the data
model
* @param integer $index the zero-based index of the data
item among the item
array returned by [[dataProvider]].
* @param integer $index the zero-based index of the data
model among the model
array returned by [[dataProvider]].
* @return string the rendering result
* @return string the rendering result
*/
*/
public
function
renderTableRow
(
$
item
,
$key
,
$index
)
public
function
renderTableRow
(
$
model
,
$key
,
$index
)
{
{
$cells
=
array
();
$cells
=
array
();
/** @var \yii\widgets\grid\Column $column */
/** @var \yii\widgets\grid\Column $column */
foreach
(
$this
->
columns
as
$column
)
{
foreach
(
$this
->
columns
as
$column
)
{
$cells
[]
=
$column
->
renderDataCell
(
$
item
,
$index
);
$cells
[]
=
$column
->
renderDataCell
(
$
model
,
$index
);
}
}
if
(
$this
->
rowOptions
instanceof
Closure
)
{
if
(
$this
->
rowOptions
instanceof
Closure
)
{
$options
=
call_user_func
(
$this
->
rowOptions
,
$
item
,
$key
,
$index
);
$options
=
call_user_func
(
$this
->
rowOptions
,
$
model
,
$key
,
$index
);
}
else
{
}
else
{
$options
=
$this
->
rowOptions
;
$options
=
$this
->
rowOptions
;
}
}
...
@@ -315,10 +315,10 @@ class GridView extends ListViewBase
...
@@ -315,10 +315,10 @@ class GridView extends ListViewBase
protected
function
guessColumns
()
protected
function
guessColumns
()
{
{
$
items
=
$this
->
dataProvider
->
getItem
s
();
$
models
=
$this
->
dataProvider
->
getModel
s
();
$
item
=
reset
(
$item
s
);
$
model
=
reset
(
$model
s
);
if
(
is_array
(
$
item
)
||
is_object
(
$item
))
{
if
(
is_array
(
$
model
)
||
is_object
(
$model
))
{
foreach
(
$
item
as
$name
=>
$value
)
{
foreach
(
$
model
as
$name
=>
$value
)
{
$this
->
columns
[]
=
$name
;
$this
->
columns
[]
=
$name
;
}
}
}
else
{
}
else
{
...
...
framework/yii/widgets/ListView.php
View file @
b31b02a5
...
@@ -19,7 +19,7 @@ use yii\helpers\Html;
...
@@ -19,7 +19,7 @@ use yii\helpers\Html;
class
ListView
extends
ListViewBase
class
ListView
extends
ListViewBase
{
{
/**
/**
* @var array the HTML attributes for the container of the rendering result of each data
item
.
* @var array the HTML attributes for the container of the rendering result of each data
model
.
* The "tag" element specifies the tag name of the container element and defaults to "div".
* The "tag" element specifies the tag name of the container element and defaults to "div".
* If "tag" is false, it means no container element will be rendered.
* If "tag" is false, it means no container element will be rendered.
*/
*/
...
@@ -29,7 +29,7 @@ class ListView extends ListViewBase
...
@@ -29,7 +29,7 @@ class ListView extends ListViewBase
* for rendering each data item. If it specifies a view name, the following variables will
* for rendering each data item. If it specifies a view name, the following variables will
* be available in the view:
* be available in the view:
*
*
* - `$
item`: mixed, the data item
* - `$
model`: mixed, the data model
* - `$key`: mixed, the key value associated with the data item
* - `$key`: mixed, the key value associated with the data item
* - `$index`: integer, the zero-based index of the data item in the items array returned by [[dataProvider]].
* - `$index`: integer, the zero-based index of the data item in the items array returned by [[dataProvider]].
* - `$widget`: ListView, this widget instance
* - `$widget`: ListView, this widget instance
...
@@ -39,7 +39,7 @@ class ListView extends ListViewBase
...
@@ -39,7 +39,7 @@ class ListView extends ListViewBase
* If this property is specified as a callback, it should have the following signature:
* If this property is specified as a callback, it should have the following signature:
*
*
* ~~~
* ~~~
* function ($
item
, $key, $index, $widget)
* function ($
model
, $key, $index, $widget)
* ~~~
* ~~~
*/
*/
public
$itemView
;
public
$itemView
;
...
@@ -50,40 +50,40 @@ class ListView extends ListViewBase
...
@@ -50,40 +50,40 @@ class ListView extends ListViewBase
/**
/**
* Renders all data
item
s.
* Renders all data
model
s.
* @return string the rendering result
* @return string the rendering result
*/
*/
public
function
renderItems
()
public
function
renderItems
()
{
{
$
items
=
$this
->
dataProvider
->
getItem
s
();
$
models
=
$this
->
dataProvider
->
getModel
s
();
$keys
=
$this
->
dataProvider
->
getKeys
();
$keys
=
$this
->
dataProvider
->
getKeys
();
$rows
=
array
();
$rows
=
array
();
foreach
(
array_values
(
$
items
)
as
$index
=>
$item
)
{
foreach
(
array_values
(
$
models
)
as
$index
=>
$model
)
{
$rows
[]
=
$this
->
renderItem
(
$
item
,
$keys
[
$index
],
$index
);
$rows
[]
=
$this
->
renderItem
(
$
model
,
$keys
[
$index
],
$index
);
}
}
return
implode
(
$this
->
separator
,
$rows
);
return
implode
(
$this
->
separator
,
$rows
);
}
}
/**
/**
* Renders a single data
item
.
* Renders a single data
model
.
* @param mixed $
item the data item
to be rendered
* @param mixed $
model the data model
to be rendered
* @param mixed $key the key value associated with the data
item
* @param mixed $key the key value associated with the data
model
* @param integer $index the zero-based index of the data
item in the item
array returned by [[dataProvider]].
* @param integer $index the zero-based index of the data
model in the model
array returned by [[dataProvider]].
* @return string the rendering result
* @return string the rendering result
*/
*/
public
function
renderItem
(
$
item
,
$key
,
$index
)
public
function
renderItem
(
$
model
,
$key
,
$index
)
{
{
if
(
$this
->
itemView
===
null
)
{
if
(
$this
->
itemView
===
null
)
{
$content
=
$key
;
$content
=
$key
;
}
elseif
(
is_string
(
$this
->
itemView
))
{
}
elseif
(
is_string
(
$this
->
itemView
))
{
$content
=
$this
->
getView
()
->
render
(
$this
->
itemView
,
array
(
$content
=
$this
->
getView
()
->
render
(
$this
->
itemView
,
array
(
'
item'
=>
$item
,
'
model'
=>
$model
,
'key'
=>
$key
,
'key'
=>
$key
,
'index'
=>
$index
,
'index'
=>
$index
,
'widget'
=>
$this
,
'widget'
=>
$this
,
));
));
}
else
{
}
else
{
$content
=
call_user_func
(
$this
->
itemView
,
$
item
,
$key
,
$index
,
$this
);
$content
=
call_user_func
(
$this
->
itemView
,
$
model
,
$key
,
$index
,
$this
);
}
}
$options
=
$this
->
itemOptions
;
$options
=
$this
->
itemOptions
;
$tag
=
ArrayHelper
::
remove
(
$options
,
'tag'
,
'div'
);
$tag
=
ArrayHelper
::
remove
(
$options
,
'tag'
,
'div'
);
...
...
framework/yii/widgets/ListViewBase.php
View file @
b31b02a5
...
@@ -70,7 +70,7 @@ abstract class ListViewBase extends Widget
...
@@ -70,7 +70,7 @@ abstract class ListViewBase extends Widget
/**
/**
* Renders the data
item
s.
* Renders the data
model
s.
* @return string the rendering result.
* @return string the rendering result.
*/
*/
abstract
public
function
renderItems
();
abstract
public
function
renderItems
();
...
...
framework/yii/widgets/grid/Column.php
View file @
b31b02a5
...
@@ -75,18 +75,18 @@ class Column extends Object
...
@@ -75,18 +75,18 @@ class Column extends Object
/**
/**
* Renders a data cell.
* Renders a data cell.
* @param mixed $
item the data item
* @param mixed $
model the data model being rendered
* @param integer $index the zero-based index of the data item among the item array returned by [[dataProvider]].
* @param integer $index the zero-based index of the data item among the item array returned by [[dataProvider]].
* @return string the rendering result
* @return string the rendering result
*/
*/
public
function
renderDataCell
(
$
item
,
$index
)
public
function
renderDataCell
(
$
model
,
$index
)
{
{
if
(
$this
->
bodyOptions
instanceof
Closure
)
{
if
(
$this
->
bodyOptions
instanceof
Closure
)
{
$options
=
call_user_func
(
$this
->
bodyOptions
,
$
item
,
$index
,
$this
);
$options
=
call_user_func
(
$this
->
bodyOptions
,
$
model
,
$index
,
$this
);
}
else
{
}
else
{
$options
=
$this
->
bodyOptions
;
$options
=
$this
->
bodyOptions
;
}
}
return
Html
::
tag
(
'td'
,
$this
->
renderDataCellContent
(
$
item
,
$index
),
$options
);
return
Html
::
tag
(
'td'
,
$this
->
renderDataCellContent
(
$
model
,
$index
),
$options
);
}
}
/**
/**
...
@@ -121,14 +121,14 @@ class Column extends Object
...
@@ -121,14 +121,14 @@ class Column extends Object
/**
/**
* Renders the data cell content.
* Renders the data cell content.
* @param mixed $
item the data item
* @param mixed $
model the data model
* @param integer $index the zero-based index of the data
item among the item
array returned by [[dataProvider]].
* @param integer $index the zero-based index of the data
model among the models
array returned by [[dataProvider]].
* @return string the rendering result
* @return string the rendering result
*/
*/
protected
function
renderDataCellContent
(
$
item
,
$index
)
protected
function
renderDataCellContent
(
$
model
,
$index
)
{
{
if
(
$this
->
content
!==
null
)
{
if
(
$this
->
content
!==
null
)
{
return
call_user_func
(
$this
->
content
,
$
item
,
$index
,
$this
);
return
call_user_func
(
$this
->
content
,
$
model
,
$index
,
$this
);
}
else
{
}
else
{
return
$this
->
grid
->
emptyCell
;
return
$this
->
grid
->
emptyCell
;
}
}
...
...
framework/yii/widgets/grid/DataColumn.php
View file @
b31b02a5
...
@@ -37,7 +37,6 @@ class DataColumn extends Column
...
@@ -37,7 +37,6 @@ class DataColumn extends Column
* If this property is an array, a dropdown list will be generated that uses this property value as
* If this property is an array, a dropdown list will be generated that uses this property value as
* the list options.
* the list options.
* If you don't want a filter for this data column, set this value to false.
* If you don't want a filter for this data column, set this value to false.
* @since 1.1.1
*/
*/
public
$filter
;
public
$filter
;
...
@@ -49,10 +48,10 @@ class DataColumn extends Column
...
@@ -49,10 +48,10 @@ class DataColumn extends Column
if
(
$this
->
enableSorting
&&
(
$sort
=
$provider
->
getSort
())
!==
false
&&
$sort
->
hasAttribute
(
$this
->
attribute
))
{
if
(
$this
->
enableSorting
&&
(
$sort
=
$provider
->
getSort
())
!==
false
&&
$sort
->
hasAttribute
(
$this
->
attribute
))
{
return
$sort
->
link
(
$this
->
attribute
);
return
$sort
->
link
(
$this
->
attribute
);
}
}
$
items
=
$provider
->
getItem
s
();
$
models
=
$provider
->
getModel
s
();
if
((
$
item
=
reset
(
$item
s
))
instanceof
Model
)
{
if
((
$
model
=
reset
(
$model
s
))
instanceof
Model
)
{
/** @var Model $
item
*/
/** @var Model $
model
*/
return
$
item
->
getAttributeLabel
(
$this
->
attribute
);
return
$
model
->
getAttributeLabel
(
$this
->
attribute
);
}
elseif
(
$provider
instanceof
ActiveDataProvider
)
{
}
elseif
(
$provider
instanceof
ActiveDataProvider
)
{
if
(
$provider
->
query
instanceof
ActiveQuery
)
{
if
(
$provider
->
query
instanceof
ActiveQuery
)
{
/** @var Model $model */
/** @var Model $model */
...
@@ -81,14 +80,14 @@ class DataColumn extends Column
...
@@ -81,14 +80,14 @@ class DataColumn extends Column
}
}
}
}
protected
function
renderDataCellContent
(
$
item
,
$index
)
protected
function
renderDataCellContent
(
$
model
,
$index
)
{
{
if
(
$this
->
value
!==
null
)
{
if
(
$this
->
value
!==
null
)
{
$value
=
call_user_func
(
$this
->
value
,
$
item
,
$index
,
$this
);
$value
=
call_user_func
(
$this
->
value
,
$
model
,
$index
,
$this
);
}
elseif
(
$this
->
content
===
null
&&
$this
->
attribute
!==
null
)
{
}
elseif
(
$this
->
content
===
null
&&
$this
->
attribute
!==
null
)
{
$value
=
ArrayHelper
::
getValue
(
$
item
,
$this
->
attribute
);
$value
=
ArrayHelper
::
getValue
(
$
model
,
$this
->
attribute
);
}
else
{
}
else
{
return
parent
::
renderDataCellContent
(
$
item
,
$index
);
return
parent
::
renderDataCellContent
(
$
model
,
$index
);
}
}
return
$this
->
grid
->
formatter
->
format
(
$value
,
$this
->
format
);
return
$this
->
grid
->
formatter
->
format
(
$value
,
$this
->
format
);
}
}
...
...
tests/unit/framework/data/ActiveDataProviderTest.php
View file @
b31b02a5
...
@@ -30,7 +30,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
...
@@ -30,7 +30,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
$provider
=
new
ActiveDataProvider
(
array
(
$provider
=
new
ActiveDataProvider
(
array
(
'query'
=>
Order
::
find
()
->
orderBy
(
'id'
),
'query'
=>
Order
::
find
()
->
orderBy
(
'id'
),
));
));
$orders
=
$provider
->
get
Item
s
();
$orders
=
$provider
->
get
Model
s
();
$this
->
assertEquals
(
3
,
count
(
$orders
));
$this
->
assertEquals
(
3
,
count
(
$orders
));
$this
->
assertTrue
(
$orders
[
0
]
instanceof
Order
);
$this
->
assertTrue
(
$orders
[
0
]
instanceof
Order
);
$this
->
assertEquals
(
array
(
1
,
2
,
3
),
$provider
->
getKeys
());
$this
->
assertEquals
(
array
(
1
,
2
,
3
),
$provider
->
getKeys
());
...
@@ -41,7 +41,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
...
@@ -41,7 +41,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
'pageSize'
=>
2
,
'pageSize'
=>
2
,
)
)
));
));
$orders
=
$provider
->
get
Item
s
();
$orders
=
$provider
->
get
Model
s
();
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
count
(
$orders
));
}
}
...
@@ -52,7 +52,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
...
@@ -52,7 +52,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
'db'
=>
$this
->
getConnection
(),
'db'
=>
$this
->
getConnection
(),
'query'
=>
$query
->
from
(
'tbl_order'
)
->
orderBy
(
'id'
),
'query'
=>
$query
->
from
(
'tbl_order'
)
->
orderBy
(
'id'
),
));
));
$orders
=
$provider
->
get
Item
s
();
$orders
=
$provider
->
get
Model
s
();
$this
->
assertEquals
(
3
,
count
(
$orders
));
$this
->
assertEquals
(
3
,
count
(
$orders
));
$this
->
assertTrue
(
is_array
(
$orders
[
0
]));
$this
->
assertTrue
(
is_array
(
$orders
[
0
]));
$this
->
assertEquals
(
array
(
0
,
1
,
2
),
$provider
->
getKeys
());
$this
->
assertEquals
(
array
(
0
,
1
,
2
),
$provider
->
getKeys
());
...
@@ -65,7 +65,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
...
@@ -65,7 +65,7 @@ class ActiveDataProviderTest extends DatabaseTestCase
'pageSize'
=>
2
,
'pageSize'
=>
2
,
)
)
));
));
$orders
=
$provider
->
get
Item
s
();
$orders
=
$provider
->
get
Model
s
();
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
2
,
count
(
$orders
));
}
}
}
}
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