Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting one to many relationship on sonata admin

I have tried to search online for a definite solution to this question but there's really no concrete solution for newbies out there.

I have an Entry which has many EntryListing. In my EntryAdmin listMapper, i comfortably can list entries by a statement as simple as

->add('listings')

which simply returns the listings as defined in the EntryListing __toString() function.

Is there a way to achieve the same when exporting the data by overiding the getExportFields() functions as below:

public function getExportFields()
{
    return array('name','tel','email','deviceType','postedOn','createdAt','facilitator','listings');
}

Your help will be very much appreciated

like image 213
Masinde Muliro Avatar asked Oct 04 '14 04:10

Masinde Muliro


1 Answers

There is another work around you can add a property in your entity which will get all entry listing related to that and in getter function return __toString() of related ones, I had the same scenario for orders and also need the list if products associated with order so i have done it this way by creating exportProducts in orders entity

protected $exportProducts;

public function getExportProducts()
{
    $exportProducts = array();
    $i = 1;
    foreach ($this->getItems() as $key => $val) {
        $exportProducts[] = $i . 
           ') Name:' . $val->getProduct()->__toString()() . 
           ' Size:' . $val->getProductsize() .
           .../** Other properties */;
        $i++;
    }
    return $this->exportProducts = join(' , ', $exportProducts);
}

And in order admin class i defined exportProducts property in getExportFields() as

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

In downloaded csv each order contains the list of products under Products cell as comma separated list

like image 154
M Khalid Junaid Avatar answered Oct 06 '22 01:10

M Khalid Junaid