简体   繁体   English

如何在 SonataAdminBundle 中自定义导出

[英]How to custom export in SonataAdminBundle

I am trying to find a way to customize the export csv on my entity admin.我正在尝试找到一种在我的实体管理员上自定义导出 csv 的方法。

Ideally, I need to extract the data from the functions of my entity to put it in the csv file.理想情况下,我需要从实体的函数中提取数据以将其放入 csv 文件中。

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.我列出了要使用 configureExportFields() 函数提取的字段,并且我尝试使用 getDataSourceIterator() 方法修改请求,但似乎导出出错。

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在 PHP 8.0.7 上

Ok, I found this solution, Sonata admin export fields with collection fields .好的,我找到了这个解决方案, 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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