GasKa
GasKa

Reputation: 663

Sonata admin export fields with collection fields

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.

Upvotes: 3

Views: 3427

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

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

Upvotes: 3

Related Questions