在Symfony Admin中使用多个连接到Excel的Mysql查询

时间:2011-10-16 23:07:55

标签: mysql excel symfony1 doctrine

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

我现在正在寻找从管理员生成的后端创建所有与会者的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 

正如您所看到的,标准和外部联接很少。

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

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

注意:Symfony 1.4与Doctrine。

1 个答案:

答案 0 :(得分:0)

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

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

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

$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 /

,则有用