CSqlDataProvider Undefined index: id

Hi,

I used a CSqlDataProvider class with query for a few tables with join clause that don’t have a id column, because they don’t need it. Unfortunately, I’m getting error ‘Undefined index: id’. Query is simple:

SELECT t., t1.

FROM tableA t

JOIN tableB t1 ON t1.unique_id=t.unique_id AND t1.instance_id=t.instance_id

(

unique_id is indeed a varchar(13) type field filled by uniqueid() function in php

instance_id is a foreign key

)

Is anything I can do to omit the error?

Thanks,

BarBQ

Hey, I’m having the same problem with CSqlDataProvider. What needs to be specified for this id parameter? The API says it’s just a unique identifier for the dataProvider, but nothing I do seems to work and it doesn’t appear to be optional.

This is roughly what I’m trying to accomplish.




//sample sql showing the fields i'm retrieving

$sql = 'select t.tech_name, j.tech_time from journal j left join tech t on j.tech_id = t.tech_id';


$dataProvider = new CSqlDataProvider($sql, array(

 'id'=>'??',//<--HERE IS MY PROBLEM.

 'totalItemCount'=>$itemCount,

 'sort'=>$sort,

 'pagination'=>array(

  'pageSize'=>20,

 ),

));



I haven’t used this… but check the keyField property… it’s set by default to ‘id’

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

keyField got rid of the undefined index issue. thanks.

Ok, so it is necessary to have a key field in a query. Thanks!

I am going to spell this out for anybody who runs across this error in the future.

The key field in the dataProvider defaults to ‘id’. So if you do not have a column in your query called ‘id’ you need to specify the key field




$dataProvider = new CSqlDataProvider($sql, array(

    'keyField' => [name of a column that is in your query]

));



The reason you are getting the error ‘Undefined index: id’ is because the dataProvider is looking for a column named ‘id’

Thank you so much!

Conclusion:

The keyField have to be the name of one of the columns into the SqlDataProvider.

Yes, it’s mentioned in the docs comments, but imo it would be worth to put the information in the documentation.

Hi, i ran into the same problem and according to this topic, naming the keyfield option solved my problem.

Thanks everybody

bye

Laszlo from Hungary

Thanks a lot!! you saved me a lot of searching time…!

Cheerz!

Exactly what I’m searching for.