SonataAdminBundle Exporter issue with associated objects

There is a standard function in sonata-admin-bundle for exporting data using an exporter; But how to make the current export entity and display the ManyToOne object with it?

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

UPD: In docs there is only todo

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

/**
 * 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;
}
+3
source share
1 answer

I created my own source iterator inherited from DoctrineORMQuerySourceIterator.

getValue - Traversable i, getValue recursive "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
....

( ).

+8

Source: https://habr.com/ru/post/1524688/


All Articles