doupeizheng3918 2016-12-30 11:31
浏览 52
已采纳

Zend Framework 2 - 警告:SplFileObject :: setCsvControl():escape必须是一个字符

Hi I am trying to get the csv file data and display that data in my view .

Below is my controller function :

 public function indexAction()
    {  
      $delimiter = ',';
      $enclosure = '""';
      $escape = '//';
      $filename = 'data' . DIRECTORY_SEPARATOR . 'csv' . DIRECTORY_SEPARATOR . 'StoreDepartment.csv';;
      $this->file = new SplFileObject($filename);
      $this->file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);

      $this->file->setCsvControl($delimiter, $enclosure, $escape);
      $this->useFirstRecordAsHeader = true;

      $response = $this->getResponse();
      $headers  = $response->getHeaders();
      $headers->addHeaderLine('Content-Type', 'text/csv');
      $contents = $this->file->fread($this->file->getSize());
      $response->setContent($contents);
      $views = new ViewModel(array('text'=>$response));
      return $views;       
    }

I am getting all the data as csv. but i am getting below warning :

Warning: SplFileObject::setCsvControl(): escape must be a character

Also i want to convert this data into JSON or Array format is it possible ?

Main concept is i want to store this data into my database table. So what format is suitable to store data in database ? Please Help

How to resolve both the errors ?

  • 写回答

1条回答 默认 最新

  • drr25281 2016-12-30 13:55
    关注

    The warning is because your $escape variable is two characters not one.

    $escape = '/';
    

    or

    $escape = "//";
    

    Since you only need to escape backslashes when using double quotes.

    As for your question regarding how to store in database. It's very hard to say without knowing what you need to do and what data is in the CSV. However I would suggest having a one to one mapping between a CSV column and a database column. Though the number of tables you need would really depend on the data and use cases. There are also a few situations when you may want to just store the raw data but again more information would be required and it would still be an opinionated answer that would be given

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置