繁体   English   中英

在Symfony Admin中具有多个联接到Excel的MySQL查询

[英]Mysql Query with Multiple Joins to Excel in Symfony Admin

我创建了一个不错的注册前端应用程序,用于注册事件的新用户。

我现在正在寻找从admin生成的后端创建所有参与者的Excel导出。

目前,我已经编写了一些SQL,可以为我提供所需的确切表:

select 
CONCAT(p.first_name,' ',p.last_name) as "Registered User",
p.email_address as "Register Email",
p.country as "Register Country",
t.name as "Attendee Type" ,
CONCAT(a.first_name, ' ',a.last_name) as "Attendee",
CONCAT(disc.name, ' ',disc.sex) as "Discipline",
CONCAT(divi.category, ' ', divi.weight) as "Division",
a.sex as "Sex",
a.club_name as "Club Name",
a.accomodation as "Accommodation",
a.`sharing_with1` as "Sharing With A",
a.`sharing_with2` as "Sharing With B",
a.`flight_number` as "Flight Number",
a.`flight_datetime` as "Flight Date",
a.`flight_time` as "Flight Time",
if(a.visa > 0, 'Yes', 'No') as "Visa",
a.dob as "Date of Birth",
a.passport as "Passport",
a.`expiry_date` as "Passport Expiry"

from `profile` p, `type` t,

`attendee` AS a LEFT JOIN `division` AS divi ON (a.division_id = `divi`.id) 
LEFT JOIN discipline AS disc ON (divi.discipline_id = disc.id)

where a.profile_id = p.id
 AND a.type_id = t.id 

如您所见,几乎没有标准联接和外部联接。

我还意识到,我可以在模板中使用数据创建普通的HTML表,并更改文件头以将其定义为Excel文件...用户下载并在Excel中打开文件,却从不知道有什么区别。

我想知道的是 基于我所拥有的,例如sql和输出计划,什么是最好地利用symfony做到这一点的最佳方法?

注意:Symfony 1.4和Doctrine。

我认为CSV也是一个不错的选择,可以使用Excel打开CSV文件。

无论如何,您可以做的是在模块上创建一个新动作。 我建议您将其添加到动作参数中 (它将与“新记录”按钮一起显示)。

在您的操作中,您只需要执行您的请求即可:

$this->dbh = Doctrine_Manager::getInstance()->getConnection('doctrine');
$stmt = $this->dbh->prepare($query);
$stmt->execute();
$stmt->fetchAll();

您可以像这样添加所需的响应头:

$this->getResponse()->setHttpHeader('Content-Type', 'application/poney/excel');

如果需要,可以绕开模板:

return $this->renderText($html);

如果您尝试提供CSV \\ o /,则很有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM