Sonata admin export fields with collection fields

Deadly 提交于 2021-01-27 20:01:25

问题


I'm trying to make custom columns for export, but I can't access children. Is there any possibility to do that ?

My code at this moment looks like this:

public function getExportFields()
{
    return [
        'ID'                        => 'id',
        'Transaction number'        => 'transactionNumber',
        'Loan account'              => 'loan',
        'Loan name'                 => 'loan.name',
        'Amount'                    => 'amount',
        //'Amount ($)'                => '',
        'Transaction type'          => 'transactionCategory',
        'Reference'                 => 'transactionAssociation.cashTransaction.transactionNumber',
        'Date'                      => 'date'
    ];
}

I can't find out a solution. I was thinking to use PropertyAccess, but I don't know how to integrate it here.

I'm using Symfony 3.X with Sonata.


回答1:


To get the collection records in export you cannot directly do this by specifying the property with association, A workaround for to achieve this you can define a new unmapped property in your entity with a getter function which will get all the collection details like in your main entity define new property as

protected $cashTransactionNumber;

public function getCashTransactionNumber()
{
    $cashTransactionNumber = array();
    $i = 1;
    foreach ($this->getTransactionAssociation() as $key => $transactionAssociation) {
        $cashTransactionNumber [] = $i . 
           ') No.:' . $transactionAssociation->somemethod()->__toString()() . 
           /** Other properties */;
        $i++;
    }
    return $this->cashTransactionNumber = join(' , ', $cashTransactionNumber );
}

then in your getExportFields() method call this property

public  function getExportFields(){
    return array(
        'Reference'=>'cashTransactionNumber ',
         ....// Other properties
        );
}

Reference: Exporting one to many relationship on sonata admin



来源:https://stackoverflow.com/questions/46442732/sonata-admin-export-fields-with-collection-fields

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