SonataAdminBundle导出器与映射实体的问题 [英] SonataAdminBundle Exporter issue with mapped entities
问题描述
sonata-admin-bundle中有一个标准功能,可以使用导出器导出数据;但是如何使导出当前实体和映射ManyToOne实体?
基本上我想要的是下载与ListFields中定义的完全相同的数据。
UPD:在 docs ,只有todo
UPD2:我找到了一个解决方案,但我不认为这是最好的一个:
/ **
*从映射实体添加一些字段;最简单的方法;
* @return array
* /
public function getExportFields(){
$ fieldsArray = $ this-> getModelManager() - > getExportFields($ this-> getClass ());
//这里我们添加一些魔术:)
$ fieldsArray [] ='user.superData';
$ fieldsArray [] ='user.megaData';
return $ fieldsArray;
}
我创建了自己的源迭代器继承来自DoctrineORMQuerySourceIterator。
如果方法getValue中的值是数组或Traversable的实例,我调用方法getValue递归获取每个许多实体的值:
protected function getValue($ value)
{
//如果值为数组或集合,则创建字符串
if (is_array($ value)或$ value instanceof \Traversable){
$ result = [];
foreach($ value as $ item){
$ result [] = $ this-> getValue($ item);
}
$ value = implode(',',$ result);
// formated datetime output
} elseif($ value instanceof \DateTime){
$ value = $ this-> dateFormater-> format($ value);
} elseif(is_object($ value)){
$ value =(string)$ value;
}
return $ value;
}
在您的管理类中,必须覆盖getDataSourceIterator方法以返回自己的迭代器。 / p>
这个
$ this-> getModelManager() - > getExportFields($这 - >的getClass());
返回所有实体项。更好的做法是在方法getExportFields()中创建导出项的显式列表。
public function getExportFields()
{
return [
$ this-> getTranslator() - > trans('item1_label_text')=> 'entityItem1',
$ this-> getTranslator() - > trans('item2_label_text')=> 'entityItem2.subItem',
// subItem after dot是相关实体的特定值
....
数组键入用于导出表头(这里是traslated)。
There is a standard feature in sonata-admin-bundle to export data using exporter; But how to make export current entity AND mapped ManyToOne entity with it?
Basically what I want, is to download exactly same data as defined in ListFields.
UPD: In docs, there is only todo
UPD2: I've found one solution, but I do not think it is the best one:
/**
* Add some fields from mapped entities; the simplest way;
* @return array
*/
public function getExportFields() {
$fieldsArray = $this->getModelManager()->getExportFields($this->getClass());
//here we add some magic :)
$fieldsArray[] = 'user.superData';
$fieldsArray[] = 'user.megaData';
return $fieldsArray;
}
I created own source iterator inherited from DoctrineORMQuerySourceIterator.
If value in method getValue is array or instance of Traversable i call method getValue recursive to get value for each "Many" entity:
protected function getValue($value)
{
//if value is array or collection, creates string
if (is_array($value) or $value instanceof \Traversable) {
$result = [];
foreach ($value as $item) {
$result[] = $this->getValue($item);
}
$value = implode(',', $result);
//formated datetime output
} elseif ($value instanceof \DateTime) {
$value = $this->dateFormater->format($value);
} elseif (is_object($value)) {
$value = (string) $value;
}
return $value;
}
In your admin class you must override method getDataSourceIterator to return your own iterator.
This
$this->getModelManager()->getExportFields($this->getClass());
returns all entity items. Better practice is to create explicit list of exported items in method getExportFields()
public function getExportFields()
{
return [
$this->getTranslator()->trans('item1_label_text') => 'entityItem1',
$this->getTranslator()->trans('item2_label_text') => 'entityItem2.subItem',
//subItem after dot is specific value from related entity
....
Key in array is used for export table headers (here is traslated).
这篇关于SonataAdminBundle导出器与映射实体的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!