简体   繁体   English

使用Sonata Admin Bundle更改xls导出中的日期格式

[英]Change date format in xls export with Sonata Admin Bundle

I taken over responsibility for a Symfony2 application, built on the Sonata Admin Bundle , and have been asked to make a small change by the users. 我接管了一个基于Sonata Admin BundleSymfony2应用程序的责任,并被要求用户进行一些小改动。 In the xls export of a list page, the dates all appear as eg Wed, 01 Aug 2012 00:00:00 +0200 , but the Excel format is General . 在列表页面的xls导出中,日期全部显示为例如Wed, 01 Aug 2012 00:00:00 +0200 ,但Excel格式为General The users would like the data in this column to be an Excel date type, so that it is sort-able. 用户希望此列中的数据是Excel日期类型,以便它可以排序。

I have been able to find some information about export customization, but this mostly concerns choosing the list export file types, or which fields to include, rather than how to change the format in the exported document. 我已经能够找到有关导出自定义的一些信息,但这主要涉及选择列表导出文件类型或要包括的字段,而不是如何更改导出文档中的格式。 A similar question was asked here (I think) but there is no answer. 这里有一个类似的问题(我想),但没有答案。

I think this would (or should) be very simple, but it is certainly not obvious. 我认为这会(或应该)非常简单,但肯定不是很明显。 Any help would be much appreciated. 任何帮助将非常感激。

A small improvement for Marciano's answer. 对Marciano的回答略有改进。 Makes the code a bit more resilient against sonata updates. 使代码对声纳数据更新更具弹性。

public function getDataSourceIterator()
{
    $datasourceit = parent::getDataSourceIterator();
    $datasourceit->setDateTimeFormat('d/m/Y'); //change this to suit your needs
    return $datasourceit;
}

this is my code. 这是我的代码。 It's work! 是工作!

use Exporter\Source\DoctrineORMQuerySourceIterator;
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;

and function: 和功能:

/**
 * {@inheritdoc}
 */
public function getDataSourceIterator()
{

    $datagrid = $this->getDatagrid();
    $datagrid->buildPager();
    $fields=$this->getExportFields();
    $query = $datagrid->getQuery();


    $query->select('DISTINCT ' . $query->getRootAlias());
    $query->setFirstResult(null);
    $query->setMaxResults(null);



    if ($query instanceof ProxyQueryInterface) {
        $query->addOrderBy($query->getSortBy(), $query->getSortOrder());

        $query = $query->getQuery();
    }


    return new DoctrineORMQuerySourceIterator($query, $fields,'d.m.Y');
    }

just add this in your admin (overriding a method of the admin class you are extending). 只需在您的管理员中添加此项(覆盖您正在扩展的管理类的方法)。 Found it reading the code. 发现它正在阅读代码。 It's not in the docs. 它不在文档中。

public function getDataSourceIterator()
{
    $datagrid = $this->getDatagrid();
    $datagrid->buildPager();

    $datasourceit = $this->getModelManager()->getDataSourceIterator($datagrid, $this->getExportFields());
    $datasourceit->setDateTimeFormat('d/m/Y'); //change this to suit your needs

    return $datasourceit;
}

In my admin class EmployeeAdmin I use getExportFields function specifies which fields we want to export: 在我的管理类EmployeeAdmin中,我使用getExportFields函数指定我们要导出的字段:

   public function getExportFields() {
       return array(
            $this->trans('list.label_interview_date') => 'interviewDateFormatted'           
       );
}

interviewDateFormatted is actually a call to the corresponding entity (Employee) method getInterviewDateFormatted which looks like this: interviewDateFormatted实际上是对相应实体(Employee)方法getInterviewDateFormatted的调用,如下所示:

    public function getInterviewDateFormatted() {
    return ($this->interviewDate instanceof \DateTime) ? $this->interviewDate->format("Y-m-d") : "";
}

This way I can change date format or do other necessary changes to the fields I want to export. 这样我可以更改日期格式或对我要导出的字段进行其他必要的更改。

Did you managed to make it work? 你设法让它工作了吗?

Date format is defined as parameter for new DoctrineORMQuerySourceIterator.php ( https://github.com/sonata-project/exporter/blob/master/lib/Exporter/Source/DoctrineORMQuerySourceIterator.php ) 日期格式定义为新DoctrineORMQuerySourceIterator.php的参数( https://github.com/sonata-project/exporter/blob/master/lib/Exporter/Source/DoctrineORMQuerySourceIterator.php

DoctrineORMQuerySourceIterator.php is created inside getDataSourceIterator function ( https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/2705f193d6a441b9140fef0996ca392887130ec0/Model/ModelManager.php ) DoctrineORMQuerySourceIterator.phpgetDataSourceIterator函数内创建( https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/2705f193d6a441b9140fef0996ca392887130ec0/Model/ModelManager.php

Inside of Admin.php there is function calling it: Admin.php里面有函数调用它:

   public function getDataSourceIterator()
   {
        $datagrid = $this->getDatagrid();
        $datagrid->buildPager();
        return $this->getModelManager()->getDataSourceIterator($datagrid, $this->getExportFields());
    }

If you write your own getDataSourceIterator() then you can change date format. 如果您编写自己的getDataSourceIterator()则可以更改日期格式。

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

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