cyanatide
(Cyanatide)
August 10, 2011, 9:49am
1
Hi there!
I would like to display result of SQL query in something like CGridview. But Cgridview accept only a list of item and I can only have a list of array with CSqlDataProvider. Is there an other widget which display data after SQL query?
Thanks!
kokomo
(Mwerlberger85)
August 10, 2011, 1:01pm
2
Hi,
it should be possible to pass any C*DataProvider to the gridview. It doesn’t matter if it is a CSqlDataProvider or a CActiveDataProvider.
CGridView
cyanatide
(Cyanatide)
August 10, 2011, 3:16pm
3
Here is my code:
$count=Yii::app()->db->createCommand('SELECT COUNT(*) contract')->queryScalar();
$sql='SELECT * FROM contract';
$dataProvider=new CSqlDataProvider($sql, array(
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
'f_contract_id','f_site_id', 'f_expiry_date', 'f_price_unit',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
// $dataProvider->getData() will return a list of arrays.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider->getData(),
'columns'=>array(
'f_contract_id',
'f_site_id',
'f_expiry_date',
'f_price_unit',
)));
Like this I get the following error:
Without getData() I get this error:
PHP Error
Description
Undefined index: id
Source File
Z:\Webroot\lib\yii-1.1\framework\web\CSqlDataProvider.php(117)
00105:
00106: return $command->queryAll();
00107: }
00108:
00109: /**
00110: * Fetches the data item keys from the persistent data storage.
00111: * @return array list of data item keys.
00112: */
00113: protected function fetchKeys()
00114: {
00115: $keys=array();
00116: foreach($this->getData() as $i=>$data)
00117: $keys[$i]=$data[$this->keyField];
00118: return $keys;
00119: }
Have you an idea?
cyanatide
(Cyanatide)
August 11, 2011, 7:14am
5
tri:
See this thread.
/Tommy
Thanks! It’s exactly what I need!
Revelis
(Luc)
August 18, 2011, 6:13am
6
You could also use CArrayDataProvider instead of SqlDataProvider if you hasn’t linked your query to a particular model :
Check this thread
Diane
(Hwolfe20)
March 27, 2012, 3:54pm
7
Many thanks This was just what I was looking for!!!!