CDataProviderIterator

Package system.web
Inheritance class CDataProviderIterator » CComponent
Implements Iterator, Traversable, Countable
Since 1.1.13
Source Code framework/web/CDataProviderIterator.php
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

Public Methods

Method Description Defined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CDataProviderIterator
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
count() Gets the total number of items in the dataProvider. CDataProviderIterator
current() Gets the current item in the list. CDataProviderIterator
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getDataProvider() Returns the data provider to iterate over CDataProviderIterator
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getTotalItemCount() Gets the total number of items to iterate over CDataProviderIterator
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
key() Gets the key of the current item. CDataProviderIterator
next() Moves the pointer to the next item in the list. CDataProviderIterator
raiseEvent() Raises an event. CComponent
rewind() Rewinds the iterator to the start of the list. CDataProviderIterator
valid() Checks if the current position is valid or not. CDataProviderIterator

Protected Methods

Method Description Defined By
loadPage() Loads a page of items CDataProviderIterator

Property Details

dataProvider property read-only

public CDataProvider getDataProvider()

Returns the data provider to iterate over

totalItemCount property read-only

public integer getTotalItemCount()

Gets the total number of items to iterate over

Method Details

__construct() method

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.

count() method

public integer count()
{return} integer the total number of items
Source Code: framework/web/CDataProviderIterator.php#151 (show)
public function count()
{
    return 
$this->_totalItemCount;
}

Gets the total number of items in the dataProvider. This method is required by the Countable interface.

current() method

public mixed current()
{return} mixed the current item in the list
Source Code: framework/web/CDataProviderIterator.php#93 (show)
public function current()
{
    return 
$this->_items[$this->_currentIndex];
}

Gets the current item in the list. This method is required by the Iterator interface.

getDataProvider() method

public CDataProvider getDataProvider()
{return} CDataProvider the data provider to iterate over
Source Code: framework/web/CDataProviderIterator.php#64 (show)
public function getDataProvider()
{
    return 
$this->_dataProvider;
}

Returns the data provider to iterate over

getTotalItemCount() method

public integer getTotalItemCount()
{return} integer the total number of items to iterate over
Source Code: framework/web/CDataProviderIterator.php#73 (show)
public function getTotalItemCount()
{
    return 
$this->_totalItemCount;
}

Gets the total number of items to iterate over

key() method

public integer key()
{return} integer the key of the current item
Source Code: framework/web/CDataProviderIterator.php#103 (show)
public function key()
{
    
$pageSize=$this->_dataProvider->getPagination()->getPageSize();
    return 
$this->_currentPage*$pageSize+$this->_currentIndex;
}

Gets the key of the current item. This method is required by the Iterator interface.

loadPage() method

protected array loadPage()
{return} array the items from the next page of results
Source Code: framework/web/CDataProviderIterator.php#82 (show)
protected function loadPage()
{
    
$this->_dataProvider->getPagination()->setCurrentPage($this->_currentPage);
    return 
$this->_items=$this->dataProvider->getData(true);
}

Loads a page of items

next() method

public void next()
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.

rewind() method

public void rewind()
Source Code: framework/web/CDataProviderIterator.php#129 (show)
public function rewind()
{
    
$this->_currentIndex=0;
    
$this->_currentPage=0;
    
$this->loadPage();
}

Rewinds the iterator to the start of the list. This method is required by the Iterator interface.

valid() method

public boolean valid()
{return} boolean true if this index is valid
Source Code: framework/web/CDataProviderIterator.php#141 (show)
public function valid()
{
    return 
$this->key() < $this->_totalItemCount;
}

Checks if the current position is valid or not. This method is required by the Iterator interface.

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc/api/1.1/CDataProviderIterator