Get Dynamic Fields From Related Tables

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









$filename = modelname().'.csv';

foreach($provider->data as $data){ 


	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


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?

Hi I am not sure if this what you looking for

Thx, it looks OK, but it exports all of the model, not just a few fields. I’m not as much interested in the CSV part as in the ‘how to get a field from a related table dynamically’, without ugly eval() statements.

in your search method you have criteria make use of select method

add another something as following

$criteria->select = "t.column1, t.column2, t.column3"; // you can select the fields you need you can even join with other tables

Right, that would make for cleaner code! Thx