sonata-admin-bundle中有一个标准功能,可以使用导出器导出数据; 但是如何使导出当前实体和映射ManyToOne实体呢?
基本上我想要的是下载与ListFields中定义的完全相同的数据.
UPD:在文档中,只有todo
UPD2:我找到了一个解决方案,但我不认为它是最好的解决方案:
/** * 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; }
小智.. 8
我创建了自己继承自DoctrineORMQuerySourceIterator的源迭代器.
如果方法getValue中的值是Traversable的数组或实例,则调用方法getValue递归以获取每个"Many"实体的值:
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; }
在您的管理类中,您必须覆盖方法getDataSourceIterator以返回您自己的迭代器.
这个
$this->getModelManager()->getExportFields($this->getClass());
返回所有实体项.更好的做法是在方法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 ....
数组中的键用于导出表头(这里是traslated).
我创建了自己继承自DoctrineORMQuerySourceIterator的源迭代器.
如果方法getValue中的值是Traversable的数组或实例,则调用方法getValue递归以获取每个"Many"实体的值:
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; }
在您的管理类中,您必须覆盖方法getDataSourceIterator以返回您自己的迭代器.
这个
$this->getModelManager()->getExportFields($this->getClass());
返回所有实体项.更好的做法是在方法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 ....
数组中的键用于导出表头(这里是traslated).