CDataProviderIterator
CDataProviderIterator allows iteration over large data sets without holding the entire set in memory.
CDataProviderIterator iterates over the results of a data provider, starting at the first page of results and ending at the last page. It is usually only suited for use with
CActiveDataProvider.
For example, the following code will iterate over all registered users (active record class User) without running out of memory, even if there are millions of users in the database.
$dataProvider = new CActiveDataProvider("User");
$iterator = new CDataProviderIterator($dataProvider);
foreach($iterator as $user) {
echo $user->name."\n";
}
Public Properties
Property |
Type |
Description |
Defined By |
dataProvider | CDataProvider | Returns the data provider to iterate over | CDataProviderIterator |
totalItemCount | integer | Gets the total number of items to iterate over | CDataProviderIterator |
Protected Methods
Method |
Description |
Defined By |
loadPage() | Loads a page of items | CDataProviderIterator |
Property Details
public CDataProvider getDataProvider()
Returns the data provider to iterate over
public integer getTotalItemCount()
Gets the total number of items to iterate over
Method Details
public void __construct(CDataProvider $dataProvider, integer $pageSize=NULL) |
$dataProvider | CDataProvider | the data provider to iterate over |
$pageSize | integer | pageSize to use for iteration. This is the number of objects loaded into memory at the same time. |
Source Code: framework/web/CDataProviderIterator.php#48 (
show)
public function __construct(CDataProvider $dataProvider, $pageSize=null)
{
$this->_dataProvider=$dataProvider;
$this->_totalItemCount=$dataProvider->getTotalItemCount();
if(($pagination=$this->_dataProvider->getPagination())===false)
$this->_dataProvider->setPagination($pagination=new CPagination());
if($pageSize!==null)
$pagination->setPageSize($pageSize);
}
Constructor.
public integer count() |
{return} | integer | the total number of items |
Gets the total number of items in the dataProvider. This method is required by the Countable interface.
public mixed current() |
{return} | mixed | the current item in the list |
Gets the current item in the list. This method is required by the Iterator interface.
Returns the data provider to iterate over
public integer getTotalItemCount() |
{return} | integer | the total number of items to iterate over |
Gets the total number of items to iterate over
public integer key() |
{return} | integer | the key of the current item |
Gets the key of the current item. This method is required by the Iterator interface.
protected array loadPage() |
{return} | array | the items from the next page of results |
Loads a page of items
Source Code: framework/web/CDataProviderIterator.php#113 (
show)
public function next()
{
$pageSize=$this->_dataProvider->getPagination()->getPageSize();
$this->_currentIndex++;
if($this->_currentIndex >= $pageSize)
{
$this->_currentPage++;
$this->_currentIndex=0;
$this->loadPage();
}
}
Moves the pointer to the next item in the list. This method is required by the Iterator interface.
Rewinds the iterator to the start of the list. This method is required by the Iterator interface.
public boolean valid() |
{return} | boolean | true if this index is valid |
Checks if the current position is valid or not. This method is required by the Iterator interface.