在奏鸣曲管理员上导出一对多的关系

时间:2014-10-04 04:19:25

标签: symfony one-to-many sonata-admin symfony-sonata sonata

我试图在网上搜索这个问题的明确解决方案,但那里的新手确实没有具体的解决方案。

我有一个有很多EntryListing的Entry。在我的EntryAdmin listMapper中,我可以轻松地按语句列出条目,就像

一样简单
->add('listings')

只返回EntryListing __toString()函数中定义的列表。

有没有办法在通过覆盖getExportFields()函数导出数据时实现相同的目的:

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

非常感谢您的帮助

1 个答案:

答案 0 :(得分:7)

你还可以在你的实体中添加一个属性,它将获得与之相关的所有条目列表,并在相关的getter函数返回__toString()中,我有相同的订单方案,也需要列出与订单相关联的产品,以便我通过在exportProducts实体

中创建orders这样做
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);
}

在管理类中,我将exportProducts中的getExportFields()属性定义为

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

在下载的csv中,每个订单都包含Products单元格下的产品列表,以逗号分隔列表

相关问题