Cgrid And Csqlprovider Undefined Index: Id Error

Hi Im using CgridView with CSqlProvider with it

here is my controller file action index:

public function actionIndex()

{


        


        $connection=Yii::app()->db;

$sql="SELECT empid, firstname, middlename, lastname, title, course FROM Students";

$rowcountsql = "SELECT COUNT(*) FROM Students";

$command = $connection->createCommand($rowcountsql);

$count=$command->queryScalar(); // provide count for pagination

// create data provider that works with CGridView

$dataProvider=new CSqlDataProvider($sql, array(

‘totalItemCount’=>$count,

‘pagination’=>array(

‘pageSize’=>5,

),

));

$this->render(‘index’,array(‘dataProvider’=>$dataProvider

));

}

My index view file:

<?php

$this->widget(‘zii.widgets.grid.CGridView’, array(

‘id’=>‘customer-address-grid’,

‘dataProvider’=>$dataProvider,

‘enablePagination’=>true,

‘ajaxUpdate’=>true,

‘enableSorting’=>true,

‘columns’=>array(

‘firstname:html:First Name’,

‘lastname:html:Last Name’,

‘middlename:html:middlename’,

‘lastname:html:lastname’,

‘title:html:title’,

‘course:html:course’

),

)); ?>

and after that I received an error;

PHP notice

Undefined index: id

/var/www/YiiProject2/yii/framework/web/CSqlDataProvider.php(119)

Check this:

http://www.yiiframework.com/doc/api/1.1/CSqlDataProvider#keyField-detail

so this means that I should only use tables with id as its primarykey?

Oh I solved the problem:

$connection=Yii::app()->db;

        &#036;key=&quot;empid&quot;;

$sql="SELECT empid, firstname, middlename, lastname, title, course FROM Students";

$rowcountsql = "SELECT COUNT(*) FROM Students";

$command = $connection->createCommand($rowcountsql);

$count=$command->queryScalar(); // provide count for pagination

// create data provider that works with CGridView

$dataProvider=new CSqlDataProvider($sql, array(

'keyField' =&gt; &#036;key,

‘totalItemCount’=>$count,

‘pagination’=>array(

‘pageSize’=>5,

),

));

$this->render(‘index’,array(‘dataProvider’=>$dataProvider

));

I needto declarethe keyfield valueto my empid field,

Thanks a lot!! :rolleyes: