У меня есть механизм, позволяющий в принципе сбрасывать все в определенной модели в файл CSV.CakePHP: Экспорт индексного представления в файл CSV
public function export() {
ini_set('max_execution_time', 600); //increase max_execution_time to 10 min if data set is very large
//create a file
$filename = "export_".date("Y.m.d").".csv";
$csv_file = fopen('php://output', 'w');
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'"');
$results = $this->Requirement->find('all');
// Column headings
$header_row = array("Req #", "Identity", "Status", "Name", "Description", "Owner");
fputcsv($csv_file,$header_row,',','"');
// Loop to create CSV file rows
foreach($results as $result)
{
// Array indexes correspond to the field names in your db table(s)
$row = array(
$result['Requirement']['req_number'],
$result['Requirement']['or_identity'],
$result['Requirement']['req_status'],
$result['Requirement']['mission_id'],
$result['Requirement']['description'],
$result['Requirement']['req_owner']
);
fputcsv($csv_file,$row,',','"');
}
fclose($csv_file);
Это работает, когда нам нужно делать массовый экспорт. Теперь мне нужно сделать dump только мой индексный просмотр в CSV-файле. Это код для указательного функции в моем контроллере:
public function index(){
if(!empty($this->request->data['Requirement'])) {
$this->Session->write('Requirement', $this->request->data['Requirement']);
} else {
$this->request->data['Requirement'] = $this->Session->read('Requirement');
}
$options = array('Requirement.mission_id' => $this->request->data['Requirement']['mission_id']);
$this->Paginator->settings = $this->paginate;
$this->set('requirements', $this->Paginator->paginate('Requirement', $options));
}
И это отображаемые в представлении столбцы:
<?php foreach($requirements as $requirement): ?>
<tr>
<td width="5%"><?php echo $requirement['Requirement']['req_number']; ?></td>
<td width="15%"><?php echo $requirement['Requirement']['or_identity']; ?></td>
<td width="10%"><?php echo $status = ($requirement['Requirement']['req_status'] == 0 ? 'Open' : 'Closed'); ?></td>
<td width="15%"><?php echo $requirement['ReqIdentity']['req_name']; ?></td>
<td width="50%"><?php echo $requirement['ReqIdentity']['req_description']; ?></td>
<td width="10%"><?php echo $requirement['ReqIdentity']['req_owner']; ?></td>
</tr>
<?php endforeach; ?>
индексном фильтруется на mission_id. Как я могу использовать свою функцию экспорта, чтобы просто экспортировать то, что выводит моя индексная функция?