| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- class ArrayDataProvider extends \CDataProvider
- {
- /**
- * @var string the name of the key field. This is a field that uniquely identifies a
- * data record. In database this would be the primary key.
- * Defaults to 'id'. If it's set to false, keys of {@link rawData} array are used.
- */
- public $keyField='id';
- /**
- * @var array the data that is not paginated or sorted. When pagination is enabled,
- * this property usually contains more elements than {@link data}.
- * The array elements must use zero-based integer keys.
- */
- public $rawData=array();
- /**
- * Constructor.
- * @param array $rawData the data that is not paginated or sorted. The array elements must use zero-based integer keys.
- * @param array $config configuration (name=>value) to be applied as the initial property values of this class.
- */
- public function __construct($rawData,$config=array())
- {
- $this->rawData=$rawData;
- foreach($config as $key=>$value)
- $this->$key=$value;
- }
- protected function fetchData()
- {
- return $this->rawData;
- }
- protected function fetchKeys()
- {
- if($this->keyField===false)
- return array_keys($this->rawData);
- $keys=array();
- foreach($this->getData() as $i=>$data)
- $keys[$i]=is_object($data) ? $data->{$this->keyField} : $data[$this->keyField];
- return $keys;
- }
- protected function calculateTotalItemCount()
- {
- return count($this->rawData);
- }
- }
|