No data being returned in CSV file export

Hi,

I’m currently using the code here http://www.yiiframework.com/wiki/306/exporting-cgridview-results-to-csv-file

to try and export filterered results from a gridview into a CSV file through a button for users to download.

The download is working and the headers are populating but there is no data being included in the file too? Unless I’ve misunderstood the point of the code, shouldn’t it be returning the data in the search?

The controller


 public function actionExportPage()

    {   

        


        $model = new EventAttendees('search');

        // $model->unsetAttributes(); 


        // uncomment the following code to enable ajax-based validation

        

        if(isset($_POST['ajax']) && $_POST['ajax']==='export-attendees-form')

        {

            echo CActiveForm::validate($model);

            Yii::app()->end();

        }

        

        if (isset($_GET['EventAttendees']))

            $model->attributes = $_GET['EventAttendees'];


        if(Yii::app()->request->getParam('export')) {

            $this->actionExport();

            Yii::app()->end();

        }

        

         $this->render('exportPage',array('model'=>$model));

    }

    public function actionExport()

    {

        $fp = fopen('php://temp', 'w');

     

        /* 

         * Write a header of csv file

         */

        $headers = array(

            'user_forename',

            'user_surname',

            'user_telephone',

            'event_id',

            'status_id',

            'checkin_status_id',

            'guest_of_user_id',

            'Question1',


        );

        $row = array();

        foreach($headers as $header) {

            $row[] = EventAttendees::model()->getAttributeLabel($header);

        }

        fputcsv($fp,$row);

     

        /*

         * Init dataProvider for first page

         */

        $model=new EventAttendees('search');

        $model->unsetAttributes();  // clear any default values

        if(isset($_GET['EventAttendees'])) {

            $model->attributes=$_GET['EventAttendees'];

        }

        $dp = $model->search();

        $dp->setPagination(false);

         

        /*

         * Get models, write to a file

         */

         

        $models = $dp->getData();

        foreach($models as $model) {

            $row = array();

            foreach($headers as $head) {

                $row[] = CHtml::value($model,$head);

            }

            fputcsv($fp,$row);

        }

     

        /*

         * save csv content to a Session

         */

        rewind($fp);

        Yii::app()->user->setState('export',stream_get_contents($fp));

        fclose($fp);

    }

    public function actionExportFile()

    {

        Yii::app()->request->sendFile('export.csv',Yii::app()->user->getState('export'));

        Yii::app()->user->clearState('export');

    }

The view


Yii::app()->clientScript->registerScript('export', "

$('#export-button').on('click',function() {

    $.fn.yiiGridView.export();

});

$.fn.yiiGridView.export = function() {

    $.fn.yiiGridView.update('export-attendees-grid',{

        success: function() {

            $('#export-attendees-grid').removeClass('grid-view-loading');

            window.location = '". $this->createUrl('exportFile')  . "';

        },

        data: $('.search-form form').serialize() + '&export=true'

    });

}

");

?>


<?php echo CHtml::button('Export', array('id'=>'export-button','class'=>'button')); ?>


$this->widget('booster.widgets.TbGridView', array(

    'id' => 'export-attendees-grid',

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

    'pager' => array(

              'class' => 'booster.widgets.TbPager',

              'displayFirstAndLast' => true,

         ),

    'filter' => $model,

    'template'=>'{pager}{items}{pager}',

    'selectableRows' => 0,

    'selectionChanged' => 'function(id){ location.href = "' . $this->createUrl('view') . '/id/"+$.fn.yiiGridView.getSelection(id);}',

    'columns' => array(

        array(

            'name' => 'user_surname',

            'type' => 'raw',

            'value' => 'CHtml::link($data->user->surname, array("/onsite/eventAttend/view", "id" => $data->id))',

        ),

    ) ,

));

?>



Fixed :

The search form MUST be completed first and submitted before the export picks up anything.