2

I am in a situation like this below. I am fetching some data using CSqlDataProvider and its pagination (page size) which internally uses limit and offset. And after that I get

$rawData = $sqldp->getData();

Now I need to do some processing and adding additional data. So now I get

$modRawData = modification($rawData);

Now If I create one CArrayDataProvider to convert it into a Dataprovider so that I can use the CListView.

But My problem is here. Now I am unable to do Pagination. If I use CLinkPager its possible. But is there any other way so that I can create my own DATAPROVIDER and can handle the pagination while creating the dataProvider.

John Maclein
  • 1,034
  • 3
  • 13
  • 24

2 Answers2

2

You can instate CArrayDataProvider directly, like this:

$rawData=Yii::app()->db->createCommand('SELECT * FROM table')->queryAll(); //fetches array of associative arrays representing selected rows
            $dataProvider=new CArrayDataProvider($rawData, array(
                'id'=>'consumer', //this is an identifier for the array data provider
                'sort'=>false,
                'keyField'=>'id', //this is what will be considered your key field
                'pagination'=>array(
                    'pageSize'=>30, //eureka! you can configure your pagination from here
                ),
            ));

and you can use your $dataProvider in a CListView or CGridView now, like

$this->widget('zii.widgets.grid.CGridView',array(
    'id'=>'consumer-grid',
    'dataProvider'=>$data,
    'columns'=>array( // this array should include the attributes you want to display
        'id',
        'name',
        'additional_column_1',
    ),

));

The potential of CArrayDataProvider can match meet up your standard needs without much work, altough it might require some additional work for filtering and sorting in some cases. You can check it's documentation page to find out more.

Snivs
  • 1,105
  • 11
  • 20
  • By this approach I won't be able to use pagination of CGridView. Instead I will have to write one CLinkPager. That I don't want to do. Also I want to do some processing after fetching data from db. Think you should read the question first. – John Maclein May 19 '14 at 16:10
  • Mind if I ask the nature of the processing after fetching data from DB? – Snivs May 19 '14 at 18:32
  • @Danila Ganchar $criteria=new CDbCriteria(); $count = count(Yii::app()->db->createCommand($query)->queryAll()); $pages=new CPagination($count); $pages->pageSize=10; $pages->applyLimit($criteria); $providerreports = Yii::app()->db->createCommand($query)->queryAll($criteria); This is the code with the help I am able to create pagination, but now problem is that all record show at one page. Like if 200 record then all 200 record show on first page but according to code only 10 record should be apear – Renu Thakur Aug 14 '15 at 10:45
  • @Danila Ganchar this is my view code :- widget('CLinkPager', array( 'pages' => $pages, )) ?> – Renu Thakur Aug 14 '15 at 10:50
1

At last I got the answer. And yes I can create my custom DataProvider by extending CDataProvider. But I needed to only implement three functions

fetchData, fetchKeys, calculateTotalItemCount

And lastly you need to write your own logic into fetchData as you want.

John Maclein
  • 1,034
  • 3
  • 13
  • 24
  • Can you provide some link documenting how to extend CDataProvider and implement fetchData while using pagination and CSort? – cnvzmxcvmcx Jun 23 '14 at 18:05
  • 2
    Found that [source of CDataProvider](https://github.com/yiisoft/yii/blob/1.1.13/framework/web/CDataProvider.php) itself is quite well documented and explains this process quite well. – cnvzmxcvmcx Jun 23 '14 at 18:14