I would like to use Zii widgets for generic data models represented as objects or arrays without a database table.
In an attempt to serve an array as a data model, I tried extending CDataProvider by creating an ArrayDataProvider component as follows:
class ArrayDataProvider extends CDataProvider
{
/**
* The array containing our data. Format is:
* array (
* array('col1'=> 1, 'col2'=> 2, 'col3'=> 3, ...), // Row 1
* array('col1'=> 1, 'col2'=> 2, 'col3'=> 3, ...), // Row 2
* ...
* );
*/
public $dataArray;
public function __construct($dataArray)
{
if(empty($dataArray))
throw new CException('Empty Array given.');
$this->dataArray = $dataArray;
$this->setId(mt_rand(0, 10000)); // Without this CDataProvider expects $this->modelClass.
}
protected function fetchData()
{
return $this->dataArray;
}
protected function fetchKeys()
{
return empty($this->dataArray[0]) ? null : array_keys($this->dataArray[0]);
}
protected function calculateTotalItemCount()
{
return count($this->dataArray);
}
}
In the controller I create some test data and pass it to the view:
public function actionSearch()
{
//$dataProvider = new CActiveDataProvider('Inventory');
$data = array(
array('date' => '2010-01-19', 'itemID'=> '2', 'count'=> '5'),
array('date' => '2010-01-20', 'itemID'=> '2', 'count'=> '5'),
array('date' => '2010-01-21', 'itemID'=> '2', 'count'=> '5'),
array('date' => '2010-01-22', 'itemID'=> '2', 'count'=> '5'),
array('date' => '2010-01-23', 'itemID'=> '2', 'count'=> '5'),
array('date' => '2010-01-24', 'itemID'=> '2', 'count'=> '5'),
array('date' => '2010-01-25', 'itemID'=> '2', 'count'=> '5'),
array('date' => '2010-01-26', 'itemID'=> '2', 'count'=> '5')
);
$dataProvider = new ArrayDataProvider($data);
$this->render('search', array('dataProvider' => $dataProvider));
}
The view uses CGridView:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider
));
When I run this however, no data appears. The HTML output does contain column names but no data:
[html]
<div id="yw0" class="grid-view">
<div class="keys" style="display:none" title="/yii_app/index.php?r=mod/default/search"><span>date</span><span>itemID</span><span>count</span></div>
<div class="summary">Displaying 1-8 of 8 result(s).</div>
<table class="items">
<thead>
<tr>
</tr>
</thead>
<tbody>
<tr class="odd"></tr>
<tr class="even"></tr>
<tr class="odd"></tr>
<tr class="even"></tr>
<tr class="odd"></tr>
<tr class="even"></tr>
<tr class="odd"></tr>
<tr class="even"></tr>
</tbody>
</table>
<div class="pager"></div></div>
[/html]
Am I missing something? Currently, it appears that Zii is coupled to active records by using CActiveDataProvider. Is it possible to use Zii widgets for data whose source is not a relational database, such as a web service, LDAP directory service, or a CSV file?
Thanks,
Sunpreet.
(PS: I have seen the CArrayDataProvider extension but it still requires active records.)