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

发布于 2024-12-09 21:43:12 字数 1302 浏览 0 评论 0原文

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

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

I have created a nice registration frontend app for registering new users for an Event.

I'm now looking to create a Excel export of all attendees from the admin generated backend.

At the moment I've wrote a bit of SQL that give me the exact table I need:

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 

As you can see there are few standard and outer joins.

I also realize I can in a template create the ordiary HTML table with the data and change the file headers to define it as an Excel File... User downloads and opens the file in Excel and never knows the difference.

What I want to know is; based on what I have e.g. the sql and the output plan what is the best way to make best use of symfony to do this?

Note: Symfony 1.4 with Doctrine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是偏爱你 2024-12-16 21:43:12

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

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

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

$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/ 则很有用

I think CSV is also a nice option, csv file can be opened with Excel.

Anyway, what you can do is create a new action on your module. I suggest you to add it to the actions params (It will be displayed with the "new record" button).

In your action, you just have to execute your request:

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

You can add the response headers you need like this:

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

If you want you can bypass the templating:

return $this->renderText($html);

Usefull if you try to serv CSV \o/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文