In trying to offer the results from a Cgridview as a CSV table, I came up with this code in the controller.
// CSV export from dataprovider
// $model->search() makes a -filtered and sorted- dataprovider
// for lectures 'with' the matching professor
$provider=$model->search();
$columns=array(
'id',
'lecture_name',
'professor->name',
'start',
'end',
);
$filename = modelname().'.csv';
foreach($provider->data as $data){
$content.="\r\n";
foreach ($columns as $field){
eval("\$content.='\"'.\$data->$field.'\",';"); //<<<<<<< Is this the way to do it??
}
}
// Send csv file
Yii::app()->getRequest()->sendFile($filename, $content, "text/csv", false);
// No need to render the screen
Yii::app()->end();
After trying a while I had to take refuge in eval() to get the professor name from the related table. Although it works it’s quite a mess, especially the last 9 characters. The comma is the field separator, in case you where wondering.
It seems like the only option, but there must be other ways that my foggy brain didn’t come up with.
Does any of you bright ones know about a better ‘Yii’ way to solve this?