简体   繁体   中英

Mysql Query with Multiple Joins to Excel in Symfony Admin

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 eg 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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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