0

In sonata it is possible to export list views to different file formats e.g. excel, pdf or csv ...

This export is based on the database query running in the background. It does not consider that maybe a template is manipulating the output in the list view. For example if a timestamp is saved in the database and this field is added via

$listMapper->add('testdate', 'date', ['format' => 'y-m-d'])

on the listview its correct displayed as "2017-10-01" but in the export there is something like "2489489289" in this column...

Another example: if i render a custom column representing a state based on different properties of the current object, the state column will never appear in the exported file.

Maybe a possible solution to me could be to override the admin controller and add a custom callback function to render the export file manual.

But here is one big problem:

I have to implement the whole logic for showing the columns in the expected format also in the exporting callback function.

Is there an elegant way to achieve this? What i actually want is an export of the current list view "as it is" and not based on the database query in the background ...

Thanks

Jim Panse
  • 2,220
  • 12
  • 34

1 Answers1

1

You can add what fields you want to export or you can add functions and do your own logic for fields instead of field getters in getExportFields() method of your Admin class, read more here.

I have it like this in Admin:

public function getExportFields()
{
    return array(
        $this->trans('export.createdAt') => 'CreatedAtForExport',
        $this->trans('export.OfferPage') => 'OfferPageNameForExport'
    );
}

and in my Entity I have:

class Entity
{
        public function getCreatedAtForExport()
        {
            return $this->createdAt->format('d.m.Y H:i');
        }

        public function getOfferPageNameForExport()
        {
            return $this->isOfferPage ? 'OfferPage' : 'CalcPage';
        }
}
kunicmarko20
  • 2,095
  • 2
  • 15
  • 25
  • As far as i can see, the getExportFields() method only determines which fields are exported, no logic here. The documentation you linked has a todo comment at the bottom where the exporting templates should be described ... but this fact doesn't help me – Jim Panse Sep 28 '17 at 08:32
  • @JimPanse I edited and added code, maybe that could help you. – kunicmarko20 Sep 28 '17 at 09:12
  • I solved this problem short time after my last answer under your post and its almost likely your approach, so thank you for confirming my thoughts and the initial post :) But now I have scruples adding the "formatting" stuff to the model ... is it really ok to let the model e.g. render a string with the date format and additional information, or is that bad design? It feels a bit dirty doing this "logic" to in the model ... – Jim Panse Sep 29 '17 at 07:18
  • @JimPanse I understand your problem, I have that feeling also but I did not find another way to do this, so I just go with it. – kunicmarko20 Sep 29 '17 at 07:58