CSqlDataProvider - empty attribute

I am using CSqlDataProvider to provide the data for a gridview.

I have checked my query and it returns the data correctly, however in the gridview one of the attributes is always empty (checked using print_r($data)).

The attribute is declared correctly in my model and if I dump the CSqlDataProvider $model it’s there.

Any ideas?

Sorry if it sounds offending, but might it not be a simple typo?

What is the name of that attribute and what do you call it in the gridview?

I have thoroughly checked for typos. The name of the attribute is ‘download_average’

I have taken a dump of the SQL query, it is there and has a value in all records.

I have dumped the CSalDataProvider object and it shows there as an attribute.

I have dumped $data for each row in the gridview and it’s empty for all records.

It really is a head scratcher…

So you say that it’s working fine for the other attributes than ‘download_average’?

Quite strange.

Would you please show us the code that constructs the data provider and that for the grid view?

DataProvider:




	$sql = "SELECT dh.download_id as id, COUNT(dh.download_id) as total_download, round(count(dh.download_id)/".$cnt_avg.") as download_average, CONCAT(m.member_firstname, ' ', member_lastname) as member_name 

		FROM download_history dh

		LEFT JOIN members m ON dh.download_user_auth_id = m.member_auth_id

		WHERE 1 " . $between . $and . "

		GROUP BY m.id";

				

	$sort = new CSort();

        $sort->defaultOrder = 'download_datetime DESC'; // for initial order

        $sort->attributes = array(

            'member_name',

	    'total_download',

	    'download_average',

        );


	return new CSqlDataProvider($sql, array(

		'totalItemCount'=>$count,

		'sort' => $sort,

		'pagination'=>array(

			'pageSize'=>Yii::app()->user->getState('pageSize',50),

		),

	));



GridView:




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'download-history-grid',

	'dataProvider'=>$model->memberreport(),

	'template'=>"{summary}{pager}\n{items}\n{pager}",

        'beforeAjaxUpdate' => 'function() {showGridLoader()}',

        'afterAjaxUpdate'=>"function() {

                                                jQuery('#DownloadHistory_date_first').datepicker(jQuery.extend({showMonthAfterYear:false}, jQuery.datepicker.regional['id'], {'showAnim':'fold','dateFormat':'yy-mm-dd','changeMonth':'true','showButtonPanel':'true','changeYear':'true','constrainInput':'false'}));

                                                jQuery('#DownloadHistory_date_last').datepicker(jQuery.extend({showMonthAfterYear:false}, jQuery.datepicker.regional['id'], {'showAnim':'fold','dateFormat':'yy-mm-dd','changeMonth':'true','showButtonPanel':'true','changeYear':'true','constrainInput':'false'}));

                                                hideGridLoader();

                                        }",

	'filter'=>$model,

	'columns'=>array(

		array('name' => 'member_name', 'header' => 'Member Name', 'type' => 'raw', 'value' => '$data["member_name"]'),

		array('name' => 'total_download', 'header' => 'Total Downloads', 'type' => 'raw', 'value' => '$data["total_download"] . "<input type=\"hidden\" id=\"currentPage\" value=\"' . Yii::app()->getRequest()->getParam('page',false) . '\" />"', 'filter' => false),

		array('name' => 'download_average', 'header' => 'Average Downloads', 'type' => 'raw', 'value' => '$data["download_average"]', 'filter' => false),

		array(

                'name'=>'date',

		'header' => 'Date Range',

                'filter'=>$datePickers,

                'value'=>'',

		'htmlOptions' => array('style' => 'width:180px'),

                ),




	),

)); ?>



It looks like a problem regarding “ROUND” and “$cnt_avg” if “download_average” doesn’t work while “total_download” is working as expected.

What if you give a constant like 5 as $cnt_avg in the sql for instance?

MySQL doc says: