Yii Framework Forum: Get Dynamic Fields From Related Tables - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Get Dynamic Fields From Related Tables Refer to fields from related tables dynamically in dataprovider contex Rate Topic: -----

#1 User is offline   Binc 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 11-January 12

Posted 20 October 2012 - 02:47 PM

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?
0

#2 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 677
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 20 October 2012 - 02:58 PM

Hi I am not sure if this what you looking for

http://www.yiiframew...ts-to-csv-file/
0

#3 User is offline   Binc 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 11-January 12

Posted 20 October 2012 - 03:03 PM

View Postalirz23, on 20 October 2012 - 02:58 PM, said:

Hi I am not sure if this what you looking for

http://www.yiiframew...ts-to-csv-file/


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.
0

#4 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 677
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 20 October 2012 - 03:14 PM

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
0

#5 User is offline   Binc 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 11-January 12

Posted 20 October 2012 - 03:17 PM

View Postalirz23, on 20 October 2012 - 03:14 PM, said:

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users