简体   繁体   中英

How to custom export in SonataAdminBundle

I am trying to find a way to customize the export csv on my entity admin.

Ideally, I need to extract the data from the functions of my entity to put it in the csv file.

I list the fields I want to extract with the configureExportFields() function, and i tried to modify the request with the getDataSourceIterator() method but it seems to error the export.

If anyone know how to do this using the default sonata export

My sonata version :

sonata-project/admin-bundle              4.x-dev 9eb2c5f The missing Symfony Admin Generator
sonata-project/block-bundle              4.6.0           Symfony SonataBlockBundle
sonata-project/cache                     2.1.1           Cache library
sonata-project/doctrine-extensions       1.13.0          Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 4.x-dev 7dfe372 Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  2.7.0           Lightweight Exporter library
sonata-project/form-extensions           1.9.0           Symfony form extensions
sonata-project/twig-extensions           1.6.0           Sonata twig extensions

on PHP 8.0.7

Ok, I found this solution, Sonata admin export fields with collection fields .

You must have a property not just the function and set the property in the function return.

It works for me

protected function configureExportFields(): array
{
    return [
        
        'Preferred area' => 'user.preferredNavigationAreaAddress',
        
    ];
}

and in my entity:

protected string $preferredNavigationAreaAddress;

public function getPreferredNavigationAreaAddress(): string
{
    $preferredArea = $this->getNavigationAreas()->filter(
        function ($navigationArea) {
            return true === $navigationArea->isPreferred();
        }
    )->first();

    return $this->preferredNavigationAreaAddress = $preferredArea->getFullAddress();
}

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