我正在尝试将数据库表中的字段写入csv文件,但在下载之前没有任何内容写入该文件。我在网上搜索了任何可能有用的答案,但我找不到任何答案。我想要做的是使用PHP的SPLFileObject将数据从数据库表写入csv文件,但就像我说的那样,它不会将表中的数据写入文件。任何可以解决这个问题的帮助都将不胜感激。
这是我的代码:
public function indexAction()
{
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=reports.csv');
header('Pragma: no-cache');
header("Expires: 0");
$this->getExportService()->exportToCSVFile();
exit;
}
/**
* Setter method
* @return ExportModel
*/
public function set()
{
$this->spl_file = new SplFileObject(self::CSV_FILENAME, "w");
return $this;
}
/**
* Getter method
* @return SplFileObject|null
*/
public function get()
{
return $this->spl_file;
}
public function exportToCSVFile()
{
// get the fields from the order_records table
$rows = $this->table->select();
foreach ($rows as $fields) {
$this->get()->fputcsv(array('id' => $fields->id, 'date' => $fields->date, 'status' => $fields->status,
'payment_status' => $fields->payment_status, 'campaign_id' => $fields->campaign_id, 'tax' => $fields->tax,
'shipping' => $fields->shipping, 'payment_method' => $fields->payment_method, 'buyer_email' => $fields->buyer_email,
'paypal_payment_id' => $fields->paypal_payment_id, 'paypal_payer_id' => $fields->paypal_payer_id,
'buyer_name' => $fields->buyer_name, 'paypal_capture_id' => $fields->paypal_capture_id,
'paypal_authorization_id' => $fields->paypal_authorization_id
));
}
}