How to iterate over a dataprovider object? I want to access the \'name\' field of each row returned and build a list. Can you help?
Table structure for table
If you need itarate large data collection and you worry about memory usage do this:
$dataProvider = new CActiveDataProvider('Categories');
$iterator = new CDataProviderIterator($dataProvider);
foreach($iterator as $category) {
print_r($category);
}
CDataProviderIterator allows iteration over large data sets without holding the entire set in memory.
some performance tests
Test Time (seconds) Memory (bytes) CDbDataReader 4.9158580303192 28339952 CActiveRecord::findAll() 5.8891110420227 321388376 CDataProviderIterator 6.101970911026 31170504
Try this:
public function returnCategoryNames()
{
$dataProvider= new CActiveDataProvider('Categories');
$dataProvider->setPagination(false);
//$count = $dataProvider->totalItemCount();
$names = array();
foreach($dataProvider->getData() as $record) {
$names[] = $record->name;
}
return array_unique($names);
}
However you dont need to use a data provider, instead just use the model
foreach(Categories::model()->findAll() as $record) {
With the solution of @ben-rowe you are querying all the rows at once. You can have memory issues.
With the following solution you will fetch the categories from ten by ten (the default CPagination.pageSize value):
$dataProvider = new CActiveDataProvider('Categories', array(
'pagination' => array(
'validateCurrentPage' => false
),
));
$pagination = $dataProvider->pagination;
while ($categories = $dataProvider->getData(true)){
foreach ($categories as $category) {
//...
}
$pagination->currentPage++;
}
In Yii2 this has changed too:
$searchModel = new ModelSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
foreach($dataProvider->getModels() as $record) {
echo $record->id . '<br>';
}
exit();
Reference: getModels()