SonataAdminBundle Exporter issue with mapped entities

北慕城南 提交于 2019-12-30 10:35:29

问题


There is a standard feature in sonata-admin-bundle to export data using exporter; But how to make export current entity AND mapped ManyToOne entity with it?

Basically what I want, is to download exactly same data as defined in ListFields.

UPD: In docs, there is only todo

UPD2: I've found one solution, but I do not think it is the best one:

/**
 * 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;
}

回答1:


I created own source iterator inherited from DoctrineORMQuerySourceIterator.

If value in method getValue is array or instance of Traversable i call method getValue recursive to get value for each "Many" entity:

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;
}

In your admin class you must override method getDataSourceIterator to return your own iterator.

This

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

returns all entity items. Better practice is to create explicit list of exported items in method 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
....

Key in array is used for export table headers (here is traslated).



来源:https://stackoverflow.com/questions/21479125/sonataadminbundle-exporter-issue-with-mapped-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!