2016-03-07 47 views
1

我在symfony中制作了一个网站。本网站包含一些带有复选框选项和其他字段的表单。 复选框中的数据在刷新时序列化。 这一切都很好。Symfony Unseialize数据表单形式出口与索纳塔出口商出口

现在我必须导出这些数据,并使用Sonata项目中的数据导出器库。但数据仍然是序列化的,我在我的csv文件中有类似的东西:

a:2:{i:0; s:6:“Volets”; i:1; s:22:“ Panneau de remplissage“;}

如何反序列化我的数据以获得一个干净的csv文件?

这里是我的代码 我控制器

/** 
* @Security("has_role('ROLE_WEBFORM')") 
*/ 
public function exportAction(Request $request) 
{ 
    $filters = array(); 
    $this->handleFilterForm($request, $filters); 
    if (!$filters['webform']) { 
     throw $this->createNotFoundException(); 
    } 

    $webForm = $this->getRepository('CoreBundle:WebForm')->find($filters['webform']); 
    $source = new WebFormEntryIterator($webForm, $this->getEntityManager(), $this->get('ines_core.embedded_form.field_type_registry'), $filters); 
    return WebFormEntryExporter::createResponse('export.csv', $source); 
} 

和我的课WebFormEntryExporter

class WebFormEntryExporter 
{ 
public static function createResponse($filename, SourceIteratorInterface $source) 
{ 
    $writer  = new CsvWriter('php://output', ';', '"', "", true, true); 
    $contentType = 'text/csv'; 
    $callback = function() use ($source, $writer) { 
     $handler = \Exporter\Handler::create($source, $writer); 
     $handler->export(); 
    }; 

    return new StreamedResponse($callback, 200, [ 
     'Content-Type'  => $contentType, 
     'Content-Disposition' => sprintf('attachment; filename=%s', $filename) 
    ]); 
} 
} 

而且我WebFormEntryIterator

class WebFormEntryIterator implements SourceIteratorInterface 
{ 
    protected $em; 

    protected $registry; 

    protected $repository; 

    protected $query; 

    protected $webForm; 


    protected $iterator; 

    public function __construct(WebForm $webForm, EntityManager $em, FieldTypeRegistry $registry, array $filters) 
    { 
     $this->webForm = $webForm; 
     $this->em = $em; 
     $this->registry = $registry; 
     $this->initQuery($filters); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function current() 
    { 
     $current = $this->iterator->current(); 
     $entity = $current[0]; 

     $data = []; 
     $data['ID'] = $entity->getId(); 
     $data['Formulaire'] = $this->webForm->getName(); 
     $data['Date de création'] = date_format($entity->getCreatedAt(), 'd/m/Y H:i:s'); 
     foreach ($this->webForm->getEmbeddedFieldConfigs() as $fieldConfig) { 
      $header = $fieldConfig->getLabel(); 
      $meta = $entity->getContentMeta($fieldConfig->getName()); 

      $extension = $this->registry->get($meta->getFormat()); 
      if (method_exists($extension, 'setEntityManager')) { 
       $extension->setEntityManager($this->em); 
      } 

      $value = $extension->formatMeta($meta); 
      $data[$header] = $value; 

      unset($extension); 
     } 

     $this->query->getEntityManager()->getUnitOfWork()->detach($current[0]); 
     unset($entity); 
     unset($webForm); 

     return $data; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function next() 
    { 
     $this->iterator->next(); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function key() 
    { 
     return $this->iterator->key(); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function valid() 
    { 
     return $this->iterator->valid(); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function rewind() 
    { 
     if ($this->iterator) { 
      throw new InvalidMethodCallException('Cannot rewind a Doctrine\ORM\Query'); 
     } 

     $this->iterator = $this->query->iterate(); 
     $this->iterator->rewind(); 
    } 

    protected function initQuery(array $filters) 
    { 
     $repository = $this->em->getRepository('InesCoreBundle:Content'); 
     $qb = $repository->getWebFormEntryQueryBuilder(); 
     $repository->applyWfeFilters($qb, $filters); 
     $this->query = $qb->getQuery(); 
    } 
} 

对不起,我蹩脚的英语。

非常感谢

+0

能否请你告诉我你的WebFormEntryIterator? – chalasr

+0

在第一篇文章中添加 –

回答

0

感谢chalasr。

我必须在current()函数中在这个文件中创建tratment。 这是我做了什么:

public function current() 
{ 
    $current = $this->iterator->current(); 
    $entity = $current[0]; 

    $data = []; 
    $data['ID'] = $entity->getId(); 
    $data['Formulaire'] = $this->webForm->getName(); 
    $data['Date de création'] = date_format($entity->getCreatedAt(), 'd/m/Y H:i:s'); 
    foreach ($this->webForm->getEmbeddedFieldConfigs() as $fieldConfig) { 
     $header = $fieldConfig->getLabel(); 
     $meta = $entity->getContentMeta($fieldConfig->getName()); 

     $extension = $this->registry->get($meta->getFormat()); 
     if (method_exists($extension, 'setEntityManager')) { 
      $extension->setEntityManager($this->em); 
     } 
     $value = $extension->formatMeta($meta); 

     if($this->is_serialized($value)) { 
      $value = unserialize($value); 
      $data[$header] = implode(' | ', $value); 
     } else { 
      $data[$header] = $value; 
     } 
     unset($extension); 
    } 
    $this->query->getEntityManager()->getUnitOfWork()->detach($current[0]); 
    unset($entity); 
    unset($webForm); 

    return $data; 
} 
public function is_serialized($data) 
{ 
    if (trim($data) == "") { return false; } 
    if (preg_match("/^(i|s|a|o|d){1}:{1}(.*)/si",$data)) { return true; } 
    return false; 
}