php 如何遍历 Yii CActiveDataProvider 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12170102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:58:16  来源:igfitidea点击:

How to iterate over Yii CActiveDataProvider object?

phpmysqlhtmlyii

提问by eric

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

表/模型的表结构 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;

}

回答by Ben Rowe

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) {

回答by Akim Kelar

If you need itarate large data collection and you worry about memory usage do this:

如果您需要 itarate 大数据收集并且您担心内存使用,请执行以下操作:

$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.

CDataProviderIterator 允许对大型数据集进行迭代,而无需将整个数据集保存在内存中。

some performance tests

一些性能测试

Test                        Time (seconds)          Memory (bytes)
CDbDataReader               4.9158580303192         28339952
CActiveRecord::findAll()    5.8891110420227         321388376
CDataProviderIterator       6.101970911026          31170504

回答by Coz

In Yii2 this has changed too:

在 Yii2 中,这也发生了变化:

    $searchModel = new ModelSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    foreach($dataProvider->getModels() as $record) {
        echo $record->id . '<br>';
    }

    exit();

Reference: getModels()

参考:getModels()

回答by Jorge Barata

With the solution of @ben-rowe you are querying all the rows at once. You can have memory issues.

使用@ben-rowe 的解决方案,您可以一次查询所有行。您可能会遇到内存问题。

With the following solution you will fetch the categories from ten by ten (the default CPagination.pageSizevalue):

使用以下解决方案,您将从十乘十(默认CPagination.pageSize值)中获取类别:

        $dataProvider = new CActiveDataProvider('Categories', array(
            'pagination' => array(
                'validateCurrentPage' => false
            ),
        ));
        $pagination = $dataProvider->pagination;
        while ($categories = $dataProvider->getData(true)){
            foreach ($categories as $category) {
                //...
            }
            $pagination->currentPage++;
        }