Yii 1.1: An important tip when you are using CArrayDataProvider.

5 followers

Yii ‘s CArrayDataProvider is very helpful to display model relation data's directly on it.But it is truely a confusing one because by default it will assume a table field named “id” as primary key for its pagination purpose and what if you dont have field named “id” as primary key on your table? so its truely confusing and if you tried to display without an “id” field on ur table you will get an error like “yourmodel.id is not defined”.

To solve this CArrayDataProvider confusing problem you just need to use on of its attribute named “keyField” to specify your primary key field on your table.There is an property named “id” thats just an id of the dataProvider itself.

i have 2 models ie deal and buyer(MANY_MANY).

so $deal->buyers is the relation i used to display my deal buyers.i used a field named “facebook_id” as primary key in model “buyer” .see my code.

$dp = new CArrayDataProvider($deal->buyers, array(
   'keyField' => 'facebook_id',         // PRIMARY KEY
   'id' => 'buyers_list'                    // ID of the data provider itself
));

Regards, sirin k

Be the first person to leave a comment

Please to leave your comment.

Write new article
  • Written by: sirin k
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +8 / -1
  • Viewed: 7,222 times
  • Created on: Sep 1, 2012
  • Last updated: Sep 1, 2012
  • Tags: CArrayDataProvider