How to iterate over Yii CActiveDataProvider object?

后端 未结 4 1079
礼貌的吻别
礼貌的吻别 2020-12-28 10:17

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

4条回答
  •  囚心锁ツ
    2020-12-28 10:43

    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++;
            }
    

提交回复
热议问题