Yii Framework Forum: Problem using CArrayDataProvider and CGridView when array does not have an 'id' field. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem using CArrayDataProvider and CGridView when array does not have an 'id' field. Rate Topic: -----

#1 User is offline   curious penguin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 10-January 12

Posted 10 January 2012 - 10:30 PM

I have been trying to use CArrayDataProvider to format some array data to then display using the CGridView widget, however it does not seem to work unless there is a defined 'id' key in the array. This seems to me to be an unnecessary requirement, so I'm not sure if this is actually the case or I am missing something. As an example:


<?php $testArray1 = array(array(
			'user_id'=>'1',
			'name'=>'david',
			),
			array(
			'user_id'=>'2',
			'name'=>'andrew',
			),
	);

	$testArray2 = array(array(
			'id'=>'1',
			'name'=>'david',
			),
			array(
			'id'=>'2',
			'name'=>'andrew',
			),
	);

	$dataProvider1 = new CArrayDataProvider(
			$testArray1, array(
				'sort'=>array(
				'attributes'=>array('user_id'),
				'defaultOrder'=>array('user_id' => false),
			),
				'pagination'=>array(
				'pageSize'=>10,
			),
		));

	$dataProvider2 = new CArrayDataProvider(
			$testArray2, array(
				'sort'=>array(
				'attributes'=>array('id'),
				'defaultOrder'=>array('id' => false),
			),
				'pagination'=>array(
				'pageSize'=>10,
			),
		));

	$this->widget('zii.widgets.grid.CGridView',array('dataProvider' => $dataProvider1,));

	$this->widget('zii.widgets.grid.CGridView',array('dataProvider' => $dataProvider2,));

?>



The two arrays are exactly the same except that the id fields are labeled differently ('user_id' vs 'id'). The problem is that the array with the id field labeled 'id' will work (can be displayed by the widget CGridView), whereas that with the id field labeled 'user_id' gave me the following error:


PHP Error

Undefined index: id

yii_root/framework/web/CArrayDataProvider.php(99)

087         else
088             return $this->rawData;
089     }
090 
091     /**
092      * Fetches the data item keys from the persistent data storage.
093      * @return array list of data item keys.
094      */
095     protected function fetchKeys()
096     {
097         $keys=array();
098         foreach($this->getData() as $i=>$data)
[b]099             $keys[$i]=is_object($data) ? $data->{$this->keyField} : $data[$this->keyField];[/b]
100         return $keys;
101     }
102 
103     /**
104      * Calculates the total number of data items.
105      * This method simply returns the number of elements in {@link rawData}.
106      * @return integer the total number of data items.
107      */
108     protected function calculateTotalItemCount()
109     {
110         return count($this->rawData);
111     }



Any ideas? I can't think that it is a requirement to always have an 'id' field...
0

#2 User is offline   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 414
  • Joined: 09-February 11

Posted 10 January 2012 - 10:41 PM

Set the keyField property when you initialize the dataprovider. See the ArrayDataProvider api for details.
3

#3 User is offline   curious penguin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 10-January 12

Posted 11 January 2012 - 11:07 PM

Perfect! Thanks a lot.
0

#4 User is offline   evince 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 06-March 12

Posted 03 April 2013 - 07:25 AM

Thanks a lot.
0

#5 User is offline   Ravi Bhalodiya 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 27-August 12
  • Location:Gujarat India

Posted 06 April 2013 - 02:55 AM

Hi, I have also worked in CArrayDataProvider.

If you are using following things then no need to required id field.

$rawData=Student::model()->findAll();
$dataProvider=new CArrayDataProvider($rawData);

Try this.
Thankx..
Regards
Ravi Bhalodiya.
0

#6 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 06 April 2013 - 08:17 AM

/* moved from Bug Discussions */
"Less noise - more signal"
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users