Exporting one to many relationship on sonata admin

情到浓时终转凉″ 提交于 2019-12-22 07:13:12

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/26189569/exporting-one-to-many-relationship-on-sonata-admin

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