-
-
Save leek/7020862 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\controllers; | |
use app\models\Downloads; | |
class DownloadsController extends \lithium\action\Controller { | |
public function index() { | |
$this->request->privateKeys = array('id', 'user_id'); | |
// Dynamic conditions | |
$conditions = array(...); | |
$downloads = Downloads::find('all', array( | |
'fields' => array('user_id', 'Surveys.name'), | |
'conditions' => $conditions, | |
'with' => 'Surveys', | |
'order' => array('created' => 'desc') | |
)); | |
return compact('downloads'); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// config/bootstrap/media.php | |
use lithium\util\Set; | |
use lithium\net\http\Media; | |
// Note: I did not need the "privateKeys" functionality so it has been removed. | |
Media::type('csv', 'application/csv', array( | |
'encode' => function($data, $handler, $response) { | |
// Assume first key is the one we are dumping | |
$data = reset($data); | |
// Write | |
ob_start(); | |
$out = fopen('php://output', 'w'); | |
foreach ($data as $record) { | |
$record = Set::flatten($record); | |
if (!isset($headers)) { | |
$headers = array_keys($record); | |
fputcsv($out, $headers); | |
} | |
fputcsv($out, $record); | |
} | |
fclose($out); | |
return ob_get_clean(); | |
} | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment