CSqlDataProvider

Hi all,

Could someone explain me why CSqlDataProvider forces me to use a keyField? It’s ‘id’ by default, and it must be specified, otherwise loading of the data fails.

And what I also don’t understand, is why all these keys are listed after the table (CGridView) in a hidden <div>.

What am I supposed to do if I don’t need keys whatsoever?

Thanks!

the "keyfield" is the name of the primary key field… this is used to find the row you need in the database, without it the search would be too slow…

The key fields in the gridview are used to get the values of the primary key for the selected row…

Thank you, I know what primary key is very well. But the question remains:

In particular, what should I do if the table does not have any key?

Just add a key…

Maybe you don’t need the key… but CGridView does.

Hmmm… That doesn’t seem right I’m afraid.

CGridView can display any tabular data, right? It may be fetched from any conceivable source, eg. a complex join or a hierarchical query. In many cases it’s simply not possible to have a unique key on the result set.

What do you think of making the keyField-related functionality optional?

yktoo, tables w/o any keys are quite rare. TBH, I haven’t ever seen one in production. However, you might be able to just fetch all rows from your table and feed them to your CGridView via the CArrayDataProvider. It’s a bit of a hack, but it might be worth the try.

An example of a table without a key is a log table. It does have timestamps, but they are not at all guaranteed to be unique.

The point is CArrayDataProvider also needs a key field :angry:

Is there no way to render a dataset without a key?

Hm, okay… Yii’s very own CDbLogRoute does indeed use a primary key. If you do not feel like adding an extra key field to your table (or add all existing fields to a primary key), you might want to extend the CGridView class (in particular the CGridView.renderItems() method).