[Flow] export to .csv
Andre Hohmann
mail at andre-hohmann.net
Wed Apr 29 19:56:02 CEST 2015
Hi Mark,
thank you for the snippet. Maybe I can use something like that in other
projects, because this time I don't want a standalone view.
---
Mit freundlichen Grüssen
Andre
Am 2015-04-29 02:51, schrieb Mark Kuiphuis:
> Hi Andre,
>
> Below is a snippet of the code I am using to export information to CSV.
> I'm using a standalone view as the CSV file...
>
> $request = $this->controllerContext->getRequest();
> $package = $request->getControllerPackageKey();
> $subpackage = $request->getControllerSubpackageKey();
> $controller = ucfirst($request->getControllerName());
> $action = $request->getControllerActionName();
>
> $standaloneView = new TYPO3FluidViewStandaloneView();
> $standaloneView->setFormat('txt');
> $templatePathAndFilename = sprintf('resource://%s/Private/Templates/%s/%s/%s.%s', $package, $subpackage, $controller, ucfirst($action), $standaloneView->getFormat());
>
> // find all mitglieder with abteilung Tennis
> $records = $this->mitgliedRepository->findTennis();
> $standaloneView->setTemplatePathAndFilename($templatePathAndFilename);
> $standaloneView->assign('records', $records);
>
> if ($standaloneView) {
> $this->response->setHeader('Cache-control', 'public', TRUE);
> $this->response->setHeader('Content-type', 'application/octet-stream');
> $this->response->setHeader('Content-Description', 'File transfer', TRUE);
> $this->response->setHeader('Content-Disposition', 'attachment; filename="Export.csv"', TRUE);
>
> // As the very last thing, I send the headers to the visitor, before TYPO3 Flow comes to the part where it renders a HTML template
> $this->response->sendHeaders();
> echo $standaloneView->render();
> exit();
> }
>
> If this took place in the MitgliedController in the exportAction(), you would find the txt template of the file in the folder (but of course you can put it wherever you want):
>
> /Vendor.Package/Resources/Private/Templates/Mitglied/Export.txt
>
> Then in that file you would find something like:
>
> ------------------------------
>
> "First Name","Last Name","Email","Mobile"
> <f:for each="{records}" as="record">{record.name.firstName},{record.name.lastName},{record.email},{record.mobile}
> </f:for>
>
> ------------------------------
>
> You might need to play a bit with where you can put an enter and where not...
>
> Regards, Mark
>
> On 28/04/2015 10:39 pm, Andre Hohmann wrote:
>
>> Hallo, I want to export the class Mitglied to a csv-file. In my MitgliedRepository I make a query like this: public function findTennis() { $query = $this->createQuery(); $result = $query->matching($query->equals('abteilung', 'Tennis')) ->setOrderings(array('name' => TYPO3FlowPersistenceQueryInterface::ORDER_ASCENDING)) ->execute(); return $result; } In my controller I tried this: public function exportTennisAction() { // find all mitglieder with abteilung Tennis $records = $this->mitgliedRepository->findTennis(); $csvPath = '/var/www/apps/flow/Packages/Application/ITOOP.Atc/Resources/Private/Export/test.csv'; $fp = fopen($csvPath, 'w'); foreach ($records as $lines) { fputcsv($fp, $lines); } fclose($fp); } When I call the exportTennisAction, I get the following: #1: WARNING: FPUTCSV() EXPECTS PARAMETER 2 TO BE ARRAY, OBJECT GIVEN IN /VAR/WWW/APPS/FLOW/DATA/TEMPORARY/DEVELOPMENT/CACHE/CODE/FLOW_OBJECT_CLASSES/ITOOP_ATC_CONTROLLER_MITGLIEDCONTROLLER.PHP LINE 492 Line 492 is:
fputcsv($fp, $lines); Do I have to format the output of $records to arrays? How should I do that? Thank you!
>
> _______________________________________________
> Flow mailing list
> Flow at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/flow [1]
Links:
------
[1] http://lists.typo3.org/cgi-bin/mailman/listinfo/flow
---------------------------------------------------------------------------------------------
http://www.andre-hohmann.net
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese E-Mail. Das unerlaubte Kopieren und die unbefugte Weitergabe dieser E-Mail sind nicht gestattet.
More information about the Flow
mailing list