SonataAdminBundle导出器与映射实体的问题

 贾章鱼_977 发布于 2023-01-30 10:45

sonata-admin-bundle中有一个标准功能,可以使用导出器导出数据; 但是如何使导出当前实体和映射ManyToOne实体呢?

基本上我想要的是下载与ListFields中定义的完全相同的数据.

UPD:在文档中,只有todo

UPD2:我找到了一个解决方案,但我不认为它是最好的解决方案:

/**
 * Add some fields from mapped entities; the simplest way;
 * @return array
 */
public function getExportFields() {
    $fieldsArray = $this->getModelManager()->getExportFields($this->getClass());

    //here we add some magic :)
    $fieldsArray[] = 'user.superData';
    $fieldsArray[] = 'user.megaData';

    return $fieldsArray;
}

小智.. 8

我创建了自己继承自DoctrineORMQuerySourceIterator的源迭代器.

如果方法getValue中的值是Traversable的数组或实例,则调用方法getValue递归以获取每个"Many"实体的值:

protected function getValue($value)
{
    //if value is array or collection, creates string 
    if (is_array($value) or $value instanceof \Traversable) {
        $result = [];
        foreach ($value as $item) {
           $result[] = $this->getValue($item);
        }
        $value = implode(',', $result);
    //formated datetime output    
    } elseif ($value instanceof \DateTime) {
        $value = $this->dateFormater->format($value);
    } elseif (is_object($value)) {
        $value = (string) $value;
    }

    return $value;
}

在您的管理类中,您必须覆盖方法getDataSourceIterator以返回您自己的迭代器.

这个

$this->getModelManager()->getExportFields($this->getClass());

返回所有实体项.更好的做法是在方法getExportFields()中创建显式的导出项列表

public function getExportFields()
{       
    return [
        $this->getTranslator()->trans('item1_label_text') => 'entityItem1', 
        $this->getTranslator()->trans('item2_label_text') => 'entityItem2.subItem', 
        //subItem after dot is specific value from related entity
....

数组中的键用于导出表头(这里是traslated).

1 个回答
  • 我创建了自己继承自DoctrineORMQuerySourceIterator的源迭代器.

    如果方法getValue中的值是Traversable的数组或实例,则调用方法getValue递归以获取每个"Many"实体的值:

    protected function getValue($value)
    {
        //if value is array or collection, creates string 
        if (is_array($value) or $value instanceof \Traversable) {
            $result = [];
            foreach ($value as $item) {
               $result[] = $this->getValue($item);
            }
            $value = implode(',', $result);
        //formated datetime output    
        } elseif ($value instanceof \DateTime) {
            $value = $this->dateFormater->format($value);
        } elseif (is_object($value)) {
            $value = (string) $value;
        }
    
        return $value;
    }
    

    在您的管理类中,您必须覆盖方法getDataSourceIterator以返回您自己的迭代器.

    这个

    $this->getModelManager()->getExportFields($this->getClass());
    

    返回所有实体项.更好的做法是在方法getExportFields()中创建显式的导出项列表

    public function getExportFields()
    {       
        return [
            $this->getTranslator()->trans('item1_label_text') => 'entityItem1', 
            $this->getTranslator()->trans('item2_label_text') => 'entityItem2.subItem', 
            //subItem after dot is specific value from related entity
    ....
    

    数组中的键用于导出表头(这里是traslated).

    2023-01-30 10:47 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有