Search code examples
phpmysqlyiicgridview

How to get CGridView for the SQL Query result?


I try the following query, and which produces an array,

$user = Yii::app()->reg->createCommand()
->select('studentID')
->from('Students')
->queryAll();

But, when i try to get the value of the query in CGridview, Im getting the error,

Property "CDbCommand.0" is not defined.

here is the code for gridview,

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'students-grid',
'dataProvider'=> new CSqlDataProvider($user),
));

Solution

  • Use 'keyfield' to avoid the error, Undefined index 'id' and keyfield = name of the selected column

    $dataProvider = new CSqlDataProvider($query, array(
    'totalItemCount'=>(int) $count1,
    'keyField' => 'studentID',
    'pagination'=>array( 'pageSize'=>30, ),));