如何遍历数据提供者对象?我想访问返回的每一行的名称"字段并构建一个列表.你能帮忙吗?
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?
表/模型的表结构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;
}
试试这个:
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) {
这篇关于如何遍历 Yii CActiveDataProvider 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!