Question

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;
}
Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top