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/model categories
CREATE TABLE IF NOT EXISTS `categories` (
`idCategory` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
PRIMARY KEY (`idCategory`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=55 ;
*Function in my Controller Categories*
$names = array();
public function returnCategoryNames()
{
$dataProvider= new CActiveDataProvider('Categories');
$dataProvider->setPagination(false);
$count = $dataProvider->totalItemCount();
for($i = 0; $i < $count; $i++){
// this is where I am lost...
$myname = $dataProvider->data[$i]->name;
array_push($names, $myname);
}
return $names;
}
Try this:
However you dont need to use a data provider, instead just use the model
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):If you need itarate large data collection and you worry about memory usage do this:
CDataProviderIterator allows iteration over large data sets without holding the entire set in memory.
some performance tests
In Yii2 this has changed too:
Reference: getModels()