CakePHP:将索引视图导出为CSV文件

时间:2016-02-04 18:36:59

标签: php csv cakephp cakephp-2.x

我有一个机制可以将某个模型中的所有内容基本上转储到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);

当我们需要进行批量导出时,这是有效的。我现在需要做的是只将我的索引视图转储到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上过滤。如何使用导出功能只导出我的索引函数输出的内容?

1 个答案:

答案 0 :(得分:1)

可能的实施:

  1. 将代码从export()方法移至csv()方法,该方法根据参数内传递的数据创建CSV文件,而不是从数据库中检索它:

    public function csv($results) {
        //code for your export() method, except for the line 
        //$results = $this->Requirement->find('all');
    }
    
  2. 将您的export()方法重写为:

    public function export() {
        $results = $this->Requirement->find('all');
        $this->csv($results);
    }
    
  3. index()操作添加功能以检测导出条件:

    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;
    
        if (!empty($this->request->params['named']['export'])) {
            $this->set('requirements', $this->Paginator->paginate('Requirement', $options));
        } else {
            $this->csv($this->Paginator->paginate('Requirement', $options));
        }
    }
    
  4. 将以下行添加到index视图中:

    <?=$this->Html->link('Export',$this->here . "/export:true")?>