Csqldataprovider For Gridview Giving Error

Hello Everyone,

I am building my first application using yii framework. I want to provide data to Gridview using CSqlDataProvider.

But when I run it i get the error ‘Trying to get property of non-object’. I know CSqlDataProvider returns an array, so what is the workaround for situation like this. I have searched and read a lot of articles before posting here.

My SiteController.php contains


 public function actionIndex()

  {

    $sql = "Select [Month], TimeTaken From tbl1";

    $dataProvider=new CSqlDataProvider($sql,array('pagination'=>false));

    $this->render('index',  array('dataProvider' => $dataProvider));

  }



And my view index.php contains (yii booster widget)


    $tdata=array();

     $tdata=$dataProvider->getData();

 

    $this->widget(

    'bootstrap.widgets.TbExtendedGridView',

    array(

    'fixedHeader' => true,

    'headerOffset' => 40,

    'type' => 'striped',

    'dataProvider' => $dataProvider,

    'responsiveTable' => true,

    'template' => "{items}",

    'columns' => array(

     array('name' => 'Month', 'header'=>'Mon', 'htmlOptions'=>array('style'=>'width: 60px')),

     array('name' => 'TimeTaken', 'header'=>'Time Taken'),

        array(

     'htmlOptions' => array('nowrap'=>'nowrap'),       

     'class'=> 'bootstrap.widgets.TbButtonColumn',  

     ),

    ),

    )

    );

     var_dump($tdata) ;



Just if you want to know the data returned from query


 array(5) { [0]=> array(2) { ["Month"]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "2013-Dez" ["TimeTaken"]=> string(4) "2.73" } [1]=> array(2) { ["Month"]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "2013-Nov" ["TimeTaken"]=> string(4) "2.00" } [2]=> array(2) { ["Month"]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "2014-Feb" ["TimeTaken"]=> string(4) "4.33" } [3]=> array(2) { ["Month"]=> string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> "2014-Jan" ["TimeTaken"]=> string(5) "12.64" } [4]=> array(2) { ["Month"]=> string(9) "2014-Mär" ["TimeTaken"]=> string(4) "6.00" } } 



I am using the same data for Highchart widget too thats why I wanted it in array form and I am able to use it successfully in Highchart widget.

So how would you provide the data to Gridview if you are using custom query.

Thanks in advance for any help. Its my first post so if there are some problems in my post please do let me know I will correct them.

Try this


 $dataProvider=new CSqlDataProvider($sql,array('keyField'=>'TimeTaken','pagination'=>false));

Let me know if it works, then i can explain what the problem was.