Skip to content

Instantly share code, notes, and snippets.

@imageaid
Created December 17, 2010 17:45
Show Gist options
  • Save imageaid/745350 to your computer and use it in GitHub Desktop.
Save imageaid/745350 to your computer and use it in GitHub Desktop.
A CFWheels controller that outputs CSV data and sends a file to the user automatically
<cfcomponent extends="Controller" output="false">
<cfscript>
function init(){
// Let CFWheels know what type of output this controller can 'provide'
provides("html,xml,json,csv");
}
// I've removed all non-related methods for this sample
function export(){
if( !structKeyExists(params,"selection") ){
mailinglist = model("MailingList").findAll(where="exported=0",select="id,emailAddress");
}
else{
mailinglist = model("MailingList").findAll(select="id,emailAddress");
}
// now, loop over query and mark each email as exported
if( !structKeyExists(params,"selection") ){
for( i=1; i LTE mailinglist.recordCount; i=i+1 ){
subscriber = model("MailingList").findByKey(mailinglist['id'][i]);
subscriber.update(exported=1);
}
}
// ah, here's where the magic happens :)!
if(params.format == 'csv'){
renderWith(data=mailinglist);
}
}
</cfscript>
</cfcomponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment