我进行了api调用,该命令以csv格式从表中下载数据。但是,我的Csv中所有数据之前的第一行应该是表中所有列的名称,但是我找不到解决方案。有什么建议吗?
public function exportCSVAction()
{
$results = $this->getDoctrine()->getManager()
->getRepository('ProjectBundle:User')->findAll();
//$columns = ["id","username","email","first_name","last_name","address_one", "city","post_code","mobile","status"];
$response = new StreamedResponse();
$response->setCallback(function () use ($results) {
$handle = fopen('php://output', 'r+');
foreach ($results as $row) {
$data = array(
$row->getId(),
$row->getUsername(),
$row->getEmail(),
$row->getFirstName(),
$row->getLastName(),
$row->getAddressOne(),
$row->getCity(),
$row->getPostCode(),
$row->getMobile(),
$row->getStatus(),
);
fputcsv($handle, $data);
}
fclose($handle);
}
);
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
return $response;
}