问题描述
我试图在网上搜索这个问题的明确解决方案,但是对于新手来说,确实没有具体的解决方案.
I have tried to search online for a definite solution to this question but there's really no concrete solution for newbies out there.
我有一个条目,其中包含许多EntryListing.在我的EntryAdmin listMapper中,我可以轻松地通过一条语句来列出条目,例如
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')
仅返回EntryListing __toString()函数中定义的列表.
which simply returns the listings as defined in the EntryListing __toString() function.
在导出数据时是否有一种方法可以通过覆盖如下的getExportFields()函数来实现相同的目的:
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');
}
非常感谢您的帮助
推荐答案
还有另一种方法,您可以在实体中添加一个属性,该属性将获取与之相关的所有条目列表,并在getter函数中返回相关条目的__toString()
,我的订单场景相同,如果与订单相关的产品也需要列表,所以我通过在orders
实体中创建exportProducts
来做到这一点
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);
}
然后为了管理类,我在getExportFields()
中将exportProducts
属性定义为
And in order admin class i defined exportProducts
property in getExportFields()
as
public function getExportFields(){
return array(
'Products'=>'exportProducts',
....// Other properties
);
}
在已下载的csv中,每个订单都包含在Products
单元格下的产品列表(以逗号分隔的列表)
In downloaded csv each order contains the list of products under Products
cell as comma separated list
这篇关于在Sonata管理员上导出一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!