Yii – “Model.id” not defined for CArrayDataProvider and CSqlDataProvider etc

fetchit-21

There is a Yii error I come across every now and again, and each time it stumps me.

If you ever get SomeModel.id not defined error when trying to put your dataprovider results into a cgridview or similar then it’s likely because

1) Your model doesn’t have an id column, it might use file_id or user_id etc instead and

2) You haven’t defined which other column should be used.

We do this using what’s known as keyField:


new CArrayDataProvider($users, array('keyField' => 'user_id'));
new CSqlDataProvider($users, array('keyField' => 'user_id'));

2 comments.

  1. Thanks for sharing this piece of code, it helped me to solve a problem I had with CGridView and CArrayDataProvider.

    Cheers.

  2. Thanks, solved my problem 🙂